2

I am trying to use a CMD command prompt to copy a file from one location to multiple at roughly the same time using one line of code. Is there a way to do this using COPY, XCOPY, etc.?

I haven't been able to get this work using this type of command:

COPY C:\test.txt C:\A1\ C:\A2\

It seems like this should work, but it gives an error that the syntaxes of the command is incorrect (copy) or invalid number of parameters (xcopy).

I would like to avoid a batch file because of the way this needs to be implemented.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AAA
  • 2,388
  • 9
  • 32
  • 47

4 Answers4

2

Create a batch file that has all your target locations:

@echo off
Copy %1 targetPath1
Copy %1 targetPath2
Copy %1 targetPath3
...

Then call that with your source file path as an argument.

The call will be 1 line :-).

Martin Maat
  • 714
  • 4
  • 23
2

One line of code only? It is ugly, but possible:

for %i in ("c:\A1","c:\my folder","c:\A2") do copy test.txt %i

(If you use it within a batch file, replace every %i with %%i.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • instead of `copy test.txt %i` you should state `copy "test.txt" "%~i"` to avoid trouble with paths containing spaces and/or some other poisonous characters... – aschipfl Dec 21 '15 at 22:31
0

Another alternative -- the command separator &:

copy "test.txt" "C:\A1\" & copy "test.txt" "C:\A2\"

or:

xcopy "test.txt" "C:\A1\" & xcopy "test.txt" "C:\A2\"
aschipfl
  • 33,626
  • 12
  • 54
  • 99
0

Since the OP asked for copying the file to multiple locations at the same time, I would add this solution:

for %D in ("C:\A1\", "C:\A2\") do (start /B "Copy to %D" cmd /c "echo Copying file to %D... & copy your_file %D")

Used this to distribute a package to seven flash drives...