0

I need a script for windows to copy some files that contain a reference to a folder that has the name of the reference in their name.

Example: The file K:\examplefolder\sourcefolder\example3456file.pdf go to the folder K:\examplefolder\Destfolder\3456.

I created this:

$Src =  'K:\Escritorio\Scripts\Script_copiar_planos\Script OT\Origen'
$Dst = 'K:\Escritorio\Scripts\Script_copiar_planos\Script OT\Destino'
$file = 'K:\Escritorio\Scripts\Script_copiar_planos\Script OT\referencias.txt'

foreach ($referencia in Get-Content $file){
  Get-ChildItem  -Path $Src -Recurse -include *$referencia*.pdf -name -file | Copy-item -Destination $Dst\$referencia 
}

In referencias.txt the references are listed like:

5678
91011
121314

For me apparently is okay, but when I go to execute the script it drops the following errors:

Copy-item : No se encuentra la ruta de acceso 'K:\Escritorio\Scripts\PDF-Con-81006600-en-el-nombre - copia (2).pdf' porque no existe.
En K:\Escritorio\Scripts\Script_copiar_planos\mover.ps1: 11 Carácter: 81
+ ... referencia*.pdf -name -file | Copy-item -Destination $Dst\$referencia
+                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (K:\Escritorio\S...- copia (2).pdf:String) [Copy-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Compo
  • 36,585
  • 5
  • 27
  • 39
Kano
  • 11
  • 2
  • Does this answer your question? [How to copy local files AND PATHS to network share using PowerShell?](https://stackoverflow.com/questions/63476891/how-to-copy-local-files-and-paths-to-network-share-using-powershell) – iRon Jan 31 '21 at 17:12
  • Is it a share? You have to specify the full UNC path instead of the drive letter. Instead of `K:\``, use the dns name and/or, IP, `\\shared\drive` – Abraham Zinala Jan 31 '21 at 17:33
  • iRon: it help me in a half, with this i can copy items in the main folder but no in the sob-folders Abraham: is in local machine – Kano Jan 31 '21 at 18:01
  • no entiendo lo que estas trantando de hacer. – Abraham Zinala Jan 31 '21 at 19:52
  • 2
    @AbrahamZinala - this part of SO is for english language stuff. please use that language so that folks here can understand what you are posting. there is a spanish part of SO ... Stack Overflow en español — https://es.stackoverflow.com/ – Lee_Dailey Jan 31 '21 at 21:28
  • @zett42 You are right, thx How can put your answer as a solution? – Kano Feb 01 '21 at 08:10

1 Answers1

0

When you pass argument -name to Get-ChildItem, it outputs only the file name portion of the path, e. g. "file1.pdf". This way Copy-Item has no information about the folder of the file and will use the working directory, whatever that happens to be before the call to Get-ChildItem.

Remove the argument -name to pass the full source path to Copy-Item:

Get-ChildItem  -Path $Src -Recurse -include *$referencia*.pdf -file | Copy-item -Destination $Dst\$referencia 

As a further optimization, you could replace -include by -filter, which is more efficient. From the docs:

Filters are more efficient than other parameters. The provider applies filter when the cmdlet gets the objects rather than having PowerShell filter the objects after they're retrieved.

zett42
  • 25,437
  • 3
  • 35
  • 72