Search code examples
windowsbatch-filecmdcopy

Windows copy command copies only 1 file


In a folder, I have 4 files, for example:

install.bat
program.exe
service.exe
uninstall.bat

I am interested to know why the following command copies all bat files to C:\Temp

copy *.bat C:\Temp

But this command only copies the first file, ignoring the rest?

copy *.bat+*.exe C:\Temp

It sees all the files because the output lists them, but only one is copied.

install.bat
program.exe
service.exe
uninstall.bat
    1 file(s) copied.

Of course I can use two commands, one for each extensions, but why does it copy only one file, and how to specify multiple sources at once?

EDIT

The documentation of the command (i.e. copy /?) is the following:

Microsoft Windows [Version 10.0.19042.985]
(c) Microsoft Corporation. All rights reserved.

C:\Users\yanickrochon>copy /?
Copies one or more files to another location.

COPY [/D] [/V] [/N] [/Y | /-Y] [/Z] [/L] [/A | /B ] source [/A | /B]
     [+ source [/A | /B] [+ ...]] [destination [/A | /B]]

  source       Specifies the file or files to be copied.
  /A           Indicates an ASCII text file.
  /B           Indicates a binary file.
  /D           Allow the destination file to be created decrypted
  destination  Specifies the directory and/or filename for the new file(s).
  /V           Verifies that new files are written correctly.
  /N           Uses short filename, if available, when copying a file with a
               non-8dot3 name.
  /Y           Suppresses prompting to confirm you want to overwrite an
               existing destination file.
  /-Y          Causes prompting to confirm you want to overwrite an
               existing destination file.
  /Z           Copies networked files in restartable mode.
  /L           If the source is a symbolic link, copy the link to the target
               instead of the actual file the source link points to.

The switch /Y may be preset in the COPYCMD environment variable.
This may be overridden with /-Y on the command line.  Default is
to prompt on overwrites unless COPY command is being executed from
within a batch script.

To append files, specify a single file for destination, but multiple files
for source (using wildcards or file1+file2+file3 format).

This question is about the follow part:

COPY source + source destination

If the command accepts multiple sources, how does it work?


Solution

  • @for %e in (bat exe) do (@XCOPY "C:\source\*.%e" C:\tmp /C /S /I /F /H)
    

    Just put this to cmd and you should be fine.