Javier Ramos
Javier Ramos

Reputation: 3

How to copy array of files (3,500) from directory A to Directory B

I have 3,508 files with different names that need to copy from directory a to directory b.

Have tried:

Copy-Item "C:\users\username\directory-a\file-1.jpeg,file-2.jpeg" -Destination "C:\users\username\directory-b\"

Powershell will give error on file separation.

Upvotes: 0

Views: 92

Answers (1)

Jeff Zeitlin
Jeff Zeitlin

Reputation: 10809

PowerShell will take an array of filenames as the -Path, -LiteralPath, or -Include parameter; however, if you are providing multiple filenames separated by commas, each one must be quoted separately - your example provides only a single file name that contains commas. Instead, use

Copy-Item "C:\users\username\directory-a\file-1.jpeg","C:\users\username\directory-a\file-2.jpeg" -Destination "C:\users\username\directory-b\"

or

Copy-Item "C:\users\username\directory-a\*" -Include "file-1.jpeg","file-2.jpeg" -Destination "C:\users\username\directory-b\"

You can pass a variable that is an array of file names to any of the parameters I mentioned above, e.g.,

$files = (Get-Content C:\User\Me\List-of-files.txt)
Copy-Item -Path $Files -Destination D:\New-Folder

The documentation for Copy-Item at Microsoft Docs does not make this clear.

Upvotes: 1

Related Questions