1

I need to retrieve pdf file and copy this pdf file from source to destination. The pdf name are in txt file listspec one number by line :

100
200 
204 
79002 
XS002

I concatenate the .pdf extension

But the pdf files are 00100.pdf, 00200.pdf, 20400.pdf, 79002.pdf, XS002.pdf. I need to Padleft with 0 the pdf file name on 5 positions max.

I use this command :

Get-Content $listspec | Foreach-Object{copy-item -Path $source\$_".pdf".PadLeft(5,'0'), -destination $destination -ErrorAction SilentlyContinue -ErrorVariable +errors} 

I receive this error :

*Copy-Item : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter ' method is not supported.*

Thanks for your help.

Vincent De Smet
  • 4,859
  • 2
  • 34
  • 41

2 Answers2

1

This is a simple typo problem. Remove the comma , between -path and -destination.

Instead of this:

copy-item -Path $source\$_".pdf".PadLeft(5,'0') , -destination $destination

Use syntax like so,

copy-item -Path $source\$_".pdf".PadLeft(5,'0') -destination $destination

vonPryz
  • 22,996
  • 7
  • 54
  • 65
  • I remove comma , between -path and -destination. I receive this error _Copy-Item : A positional parameter cannot be found that accepts argument 'System.Object[]'. At C:\temp\spec\CopySpec.ps1:30 char:49 + Get-Content $listspec | Foreach-Object{copy-item <<<< -Path $source\$_".pdf".PadLeft(5,'0') -destination $destinatio n -ErrorAction SilentlyContinue -ErrorVariable +errors} + CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand_ – user3914656 Apr 30 '15 at 09:22
  • seems the foreach var is not a string, let me test – Vincent De Smet Apr 30 '15 at 14:28
0

This works for me to copy the files specified in files.txt to the folder dest:

gc .\files.txt | % { Copy-Item ($_.padRight(5,"0")+".pdf") -Destination "dest\ " }

I had to wrap the filename to have it be evaluated prior to passing it to the Copy-Item cmdlet

this: ($_.padRight(5,"0")+".pdf")

Vincent De Smet
  • 4,859
  • 2
  • 34
  • 41
  • I'm using `gc` alias for `Get-Content` cmdlet and `%` for `ForEach-Object` – Vincent De Smet Apr 30 '15 at 14:38
  • Ok, Vincent it's working. Thanks. `Get-Content $listspec | Foreach-Object{copy-item -Path ($source+$_.PadLeft(5,'0')+'.pdf') -destination $destination -ErrorAction SilentlyContinue -ErrorVariable +errors} ` – user3914656 May 03 '15 at 08:06