Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

windows - Copy multiple files from a single source to different destinations, (recursively)

I have 4 files in a single source folder (c:sandboximage).

Files are as follows, INSTALL1.SWM, INSTALL2.SWM, INSTALL3.SWM, INSTALL4.SWM.

My destination structure is c:sandboxextract***

I want to be able to copy them only if they exist in the destination folder(s). My challenge is the destination top folder is the same name all the time, however the sub folders names change so I end up with wildcards.

I have tried xcopy with the /U, and /S, parameters, but it's copying 0 files, so I think I'm missing something.

Here is what I was trying in my batch file, with no success.

xcopy c:sandboximage*.SWM c:sandboxextract /U /S
question from:https://stackoverflow.com/questions/66051040/copy-multiple-files-from-a-single-source-to-different-destinations-recursively

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
for /f "delims=" %%b in ('dir /ad /s/b "C:sandboxextract"') do for /L %%c in (1,1,4) do if exist "%%binstall%%c.swm" ECHO copy /y "c:sandboximageinstall%%c.swm" "%%binstall%%c.swm" 

Should echo the required copy commands. Remove the echo keyword to actually execute the copys. Append >nul to suppress the 1 file(s) copied message.

Untested, and assuming the requirement is to copy install?.swm over any existing versions in the subtree with root "C:sandboxextract"

[again untested - in response to comments]

for /f "delims=" %%b in ('dir /ad /s/b "C:sandboxextract"') do for %%c in ("" 2 3 4) do if exist "%%binstall%%~c.swm" ECHO copy /y "c:sandboximageinstall%%~c.swm" "%%binstall%%~c.swm" 

Note for /L replaced by plain for; list is list of suffixes to install; when used in the do clause, %%~c is used in place of %%c to remove the quotes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...