AAA
AAA

Reputation: 2450

CMD command line: copy file to multiple locations at the same time

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.

Upvotes: 2

Views: 12204

Answers (4)

Stephan
Stephan

Reputation: 56228

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

Upvotes: 2

Benno Vogel
Benno Vogel

Reputation: 11

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

Upvotes: 1

aschipfl
aschipfl

Reputation: 34979

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\"

Upvotes: 0

Martin Maat
Martin Maat

Reputation: 744

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

Upvotes: 3

Related Questions