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

耗尽温柔 提交于 2020-12-06 06:37:53

问题


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

Any advice is greatly appreciated! I would like to avoid a batch file because of the way this needs to be implemented.


回答1:


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




回答2:


one line of Code only? Ugly, but possible:

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

(if you use it within a batchfile, replace every %i with %%i)




回答3:


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



回答4:


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



来源:https://stackoverflow.com/questions/34404807/cmd-command-line-copy-file-to-multiple-locations-at-the-same-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!