Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I copy multiple named files on the Windows command line using a single "copy" command?

I'd like to copy several known files to another directory as a part of a post-build event, but don't want to have lines and lines of "copy [file] [destination] [switches]" in my build event. If possible, I'd like to list out the files I'd like to copy using a similar format: "copy [file 1] [file 2] [file 3] [etc...] [destination] [switches]". However, Windows doesn't seem to like this type of format. Any ideas? Thanks!

like image 302
Mark Carpenter Avatar asked May 28 '09 18:05

Mark Carpenter


People also ask

Can we copy multiple file by using single command?

Copy Multiple Files Using CP CommandYou must provide both the file name and the destination directory when using the cp command to copy multiple files. First, open the specific directory in the terminal and execute the tree command. If you don't know about the tree command, then please check out this blog.

Can a single copy command copy files from more than one directory?

The short answer is no. You can not use GNU/cp or BSD/cp to copy a single file to multiple directories. However, you can use combination of cp and xargs/parallel and other commands to copy a single file to multiple directories in MacOS, Linux, FreeBSD, OpenBSD, NetBSD and Unix-like systems.

How do you copy multiple files at once?

Step 1: Click on the first file to be selected. Step 2: Hold down Ctrl and click on all the files that you want to select additionally. Step 2: Press the Shift key and click on the last file. Step 3: You have now selected all files at once and can copy and move them.

How copy multiple files using xcopy?

To append files, specify a single file for destination, but multiple files for source (that is, by using wildcards or file1+file2+file3 format). If you omit destination, the xcopy command copies the files to the current directory.


3 Answers

You can use 'for' either in a batch file or directly from the command prompt:

for %I in (file1.txt file2.txt file3.txt) do copy %I c:\somedir\

Wildcards are supported in the filelist as well:

for %I in (*.txt *.doc *.html) do copy %I c:\somedir\

For more info, just type for /? from a command prompt, or for a much easier to read help use Start->Help and Support and search for "For". On my XP Pro box, it was item 15 in the full text search results.

like image 102
Ken White Avatar answered Oct 17 '22 23:10

Ken White


XP and Vista replaced xcopy with robocopy, and it will do exactly what you want. The syntax for what you want feels backwards at first, but it does the job:

robocopy source\folder a\dest\folder file1.exe file2.bat file3.dll file4.txt
like image 35
Kevin Avatar answered Oct 17 '22 21:10

Kevin


Use the <Copy> MSBuild task.

like image 2
David Schmitt Avatar answered Oct 17 '22 23:10

David Schmitt