batch file - Remove suffix from a directory's name -


a short foreword: working awkward setup; company have been contracted requires perform operations in batch, because of potential dangers of downloaded software , bureaucracy. have work purely in batch problem - no addins, downloads, or powershell suggestions please (:p).

question:

imagine directory containing directories, e.g.,

  • parent folder
    • subfolder
    • subfolder(1)
    • subfolder(2)

they've been uniquely reference such:

  • parent folder
    • ref231_subfolder
    • ref527_subfolder(1)
    • ref8837_subfolder(2)

now want remove suffixes added prevent duplicate names! so, batch's rather challenging syntax string replacement, has proved quite issue. let me walk through code have, doesn't work, may of use:

@echo off  setlocal enabledelayedexpansion  :: returns directories (/a:d), pipes findstr command, :: identifies prefix @ end of directory name. /f "eol=: delims=" %%f in ('dir /b /a:d ^| findstr ^.*^(*^)$') (    set f=%%f     set suffix=^(!f:*^(=!    echo !suffix!    set newname=!f:!suffix!=!    echo !newname! )  pause 

problem:

i expect set newname=!f:!suffix!=! replace value of !suffix! within string f, directory name, , set variable !newname!. why instead !newname! equal same !suffix!?

also, please note i've tried using %suffix%, , myriad of other variable syntaxes, no avail.

thanks help!

!var1:!var2!=! (that delayed expansion inside delayed expansion) not allowed (can not correctly parsed). , in case !var1:%var2%=! var2 value changing inside for block not work (there no delayed expansion var2).

to make code work need like

for /f "delims=" %%f in ('dir /b /a:d ^| findstr /r /c:"^.*\(.*\)$"') (     echo %%f     set "f=%%f"     set "suffix=(!f:*(=!"     echo !suffix!     /f "delims=" %%a in ("!suffix!") set "newname=!f:%%~a=!"     echo !newname! )     

that is, can not write delayed expansion inside delayed expansion, replace inner variable for replaceable parameter.

anyway, case can simplified.

for /d %%a in ( "*(*)"  ) /f "delims=(" %%b in ("%%~na" ) echo ren "%%~fa" "%%~b" 

for each folder parenthesis, take name, , split @ parenthesis. first token hold new name without suffix. ren command rename folder (full path it) new name.

ren commands echoed console. if output correct, remove echo rename folders.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -