12

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?

8
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.

| improve this answer | |
  • I confirm that it works. Maybe if it was wrapped up into a batch file you could make it shorter? So that I would only need to type stevecopy foo bar [...] folder and it would do that loop automatically. – Kevin Panko Jul 27 '10 at 15:34
  • I don't have to complete this 100% now, but for starters: for %%I in (%*) do copy %I c:\somedir\. Powershell may be a better fit for something like this, but that is an additional install. – steve.lippert Jul 27 '10 at 16:00
  • Been working on it for far too long and still can't get something to work. Best I can do for you is the answer. – steve.lippert Jul 27 '10 at 19:27
  • Thanks for the effort. I posted my version based on your idea. – Kevin Panko Jul 27 '10 at 21:33
  • 2
    You will want to wrap your second %I in double quotes as a best-practice to handle spaces in the file name – Goyuix Aug 3 '10 at 14:25
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
| improve this answer | |
4

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.)

| improve this answer | |
  • How does this help solve the problem? Can you create a wildcard for foo|bar ? – Kevin Panko Apr 13 '18 at 17:16
  • @Kevin You could use ??? if there are no other three character file names :-) The only DOS wildcard characters are * and ? so you're right, this isn't a general solution. But as the second half of my answer says, I was worried there might be people who do just want to use wildcards but get the wrong impression from the other answers here. – Arthur Tacca Apr 16 '18 at 7:46
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, ...]
| improve this answer | |
  • better: for %%in (%*) do copy ... and then the number of in files is not limited to 10 (still limited, but 255 is a bit more head room :). Oh, and then you don't need SHIFT either. More: ss64.com/nt/syntax-args.html – matt wilkie Jul 27 '10 at 23:06
  • Not sure if I understand -- do you mean to have copy %%i %1 ? That could work, but it would try to copy the first argument on top of itself, which would just create a harmless error message. – Kevin Panko Jul 28 '10 at 2:52
1

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

cp foo bar folder
| improve this answer | |
0

Try

copy foo + bar folder
| improve this answer | |
  • 1
    This is not what I wanted. This gives me one file, named folder\foo, with the contents of both foo and bar in it. I want to end up with folder\foo and folder\bar. – Kevin Panko Jul 27 '10 at 15:19
  • This concatenates the files foo and bar. Its *nix equivalent is cat foo bar > folder – ThatGraemeGuy Jul 27 '10 at 15:26
  • 1
    You mean cat foo bar > folder/foo – Kevin Panko Jul 27 '10 at 15:28
  • 1
    Yup, looks like I was mistaken. If you have access to Powershell in your Windows environment, that might be looking into. Lots of the *nix commands carry over. – Michael Jul 27 '10 at 15:32
  • Good point! I do have it. – Kevin Panko Jul 27 '10 at 15:39

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.