Can Windows' copy command handle multiple files?

On a Unix system, this command copies two files to a folder named folder:

cp foo bar folder

On Windows, this is not a valid command:

copy foo bar folder
The syntax of the command is incorrect.

Can I do this in one command on Windows, using its built-in tools?

3

6 Answers

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

You can use this in either a batch file or directly from the command line. Not as clean as *nix, but it works.

7

Windows includes robocopy built in, which copies multiple files from a single command:

robocopy a\source\folder a\dest\folder file1.docx file2.exe

It's worth noting that if you have a wildcard expression rather than an explicit list of files then COPY does copy all the files to the target directory:

COPY srcdir\* destdir\
COPY *sy?.* anotherdestdir\

Anyone coming from a *nix background would find it particularly confusing that while works while explicit lists don't, because in Unix-like shells they're indistinguishable to a program – the shell expands any wildcards, so the program ends up with an explicit file list either way. This doesn't apply here because the Windows command shell passes wildcards directly through to programs and it's up to them to do expansion. (Also COPY is a built in command in the shell rather than an external program like cp on Linux, so in principle it could potentially disobey the usual rules about command line arguments, but that actually doesn't apply here.)

2

Here is a short batch script to facilitate copying multiple files:

set FOLDER=%1
shift
for %%i in (%1 %2 %3 %4 %5 %6 %7 %8 %9) do copy %%i %FOLDER%

Usage:

my-copy <DEST-FOLDER> source [source2, source3, ...]
2

With the advent of Bash on Ubuntu on Windows, you can now use the native Linux syntax:

cp foo bar folder

Try

copy foo + bar folder
6

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like