简体   繁体   中英

Windows batch copy multiple folders with name trimming

I'm trying to copy & paste several folders inside a single folder, this is meant to be a simple backup/restore process. I can create the backup copies with the following:

for /d %%G in ("AppSettings\AppName version ?.?.?") do xcopy /e/h/k/c/i "%%~G" "%%~G_BACKUP"

I can delete them with a variation of the same using:

do rd /s /q "%%~G"

But I can't "restore" because I can't trim the "_BACKUP" from the name using the same process... the working folder name is rigid, it has to be "AppName Vesrion #.#.#" .

Any advice will be appreciated :)

why not (if original dir aren't deleted)

for /d %%G in ("AppSettings\AppName version ?.?.?") do xcopy /e/h/k/c/i "%%~G_BACKUP" "%%~G"

or

SetLocal EnableDelayedExpansion
for /d %%G in ("AppSettings\AppName version ?.?.?_BACKUP") do (
  set "originalPath=%%G" 
  set "originalPath=!originalPath:_BACKUP=!"
  xcopy /e/h/k/c/i "%%~G" "!originalPath!"
)
EndLocal

set "originalPath=!originalPath:_BACKUP=!" will remove any ocurrence of _BACKUP from originalPath

for /d %%G in ("AppSettings\AppName version ?.?.?_BACKUP") do (
  for /F "tokens=1 delims=_" %%g in ("%%~nxG") do (
    if exist "%%~dpG%%g" (
         rem next xcopy might ask Overwrite <full file path> (Yes/No/All)? 
      xcopy /e/h/k/c/i "%%~G" "%%~dpG%%g"
    ) else (
      move "%%~G" "%%g"
    )
  )
)

Resources (required reading):

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