简体   繁体   中英

Windows batch, recursively copy files

I have a large collection of images in various locations, which all sit in a folder called "global_en-us" (referring to images to be used on an EN site). These all need duplicated to various other languages (eg global_de-de) as only the EN was provided - due to the fact that nothing on the images is language specific. Obviously the best solution would be to rewrite the paths on the sites to remove references to languages but with there being 3-4 languages per collection, and somewhere between 100-120 collections, that's just too big a task and so a bat script to duplicate them all as needed would be handier.

The images are all set up similar to this. The path from "/assets/images" is the important bit, I would like to run the script from there and let it work its way down

c:/somefolder/assets/images/special-offers/some-promo-name/global_en-us/<images>
c:/somefolder/assets/images/layout/some-component-name/global_en-us/<images>
c:/somefolder/assets/images/tournaments/some-tourney-name/global_en-us/<images>

I currently have a script that I cobbled together with minimal knowledge of how bat scripting really works, which you can specify a location that contains the global_en-us folder and it will duplicate it to all required languages. I've copied it below:

@echo off
IF EXIST log.txt (
del log.txt
)

set /p folder="Enter location : "
set folderTest=%folder:~-1%

echo %folderTest% > log.txt
if %folderTest% == \ echo "slash found" > log.txt
if NOT %folderTest% == \ set folder=%folder%\

xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_bg-bg\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_pt-br\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_cs-cz\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_da-dk\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_de-de\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_el-gr\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_en-us\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_es-es\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_es-la\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_et-ee\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_fi-fi\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_fr-fr\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_hr-hr\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_hu-hu\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_is-is\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_it-it\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_ja-jp\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_lt-lt\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_lv-lv\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_nl-nl\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_no-no\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_pl-pl\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_pt-pt\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_ro-ro\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_ru-ru\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_sv-se\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_si-si\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_ua-ua\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_zh-cn\"
xcopy /Y /S "%folder%global_en-us\*.*" "%folder%global_zh-tw\"

What I would like, if this is becoming clearer now, is instead of me having to run this 100-120 times on each folder such as "c:/somefolder/assets/images/special-offers/some-promo-name/", I can tell it "c:/somefolder/assets/images/" and let it work its way resursivly, and when it finds "global_en-us" it does the xcopy as needed, and then goes back and moves on.

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q47017256.txt"

FOR /d /r "%sourcedir%\somefolder\assets\images\" %%a IN (*) DO IF /i "%%~nxa"=="global_en-us" (
 FOR /f %%d IN (%filename1%) DO (
  ECHO XCOPY /y /d /s "%%a\*" "%%~dpaglobal_%%d\"
 )
)

GOTO :EOF

You would need to change the setting of sourcedir to suit your circumstances.

I used a file named q47017256.txt containing this dummy data for my testing.

bg-bg
pt-br
cs-cz

The required XCOPY commands are merely ECHO ed for testing purposes. After you've verified that the commands are correct , change ECHO XCOPY to XCOPY to actually copy the files. Append >nul to suppress report messages (eg. 1 file copied )

The for /d /r assigns to %%a the name of each directory found starting at the nominated directory. The if permits only those directories named global_en-us at its leaf to be processed further.

Within the loop, a file of the format I've described is read and an xcopy generated to update ( /d ) the destination directory, which is constructed from the drive and path of %%a minus the leaf directory global_us-en + the literal global_ + the line read from the text file.

So you should be able to run this at any time to update the entire tree to a copy from the latest global_en-us directories.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM