@ECHO OFF & SETLOCAL enableextensions enabledelayedexpansion :: --------------------------------------------------------- :: Rename.cmd by Mark Challoner :: Renames a set of files according to a text file (one name per line) :: :: Pattern variables: :: $#: Line number :: $L: Line text (after only) :: $N: Previous name (after only) :: $X: Previous extension (after only) :: *: Any number of unknown characters (before only) :: ?: One unknown character (before only) :: --------------------------------------------------------- :: File to get names from (no quotes) SET file=Filenames.txt :: Before and after filename patterns. SET before="*.tmp" SET after="$# - $L ($N).$X" :: -------------------------------------------------------- :: Do not edit below this following line SET c1=1 IF NOT EXIST "%file%" ( ECHO %file% not found. Script will now exit. PAUSE GOTO :EOF ) FOR /F "usebackq tokens=* delims=" %%A IN ("%file%") DO ( IF !c1! LSS 10 (SET number=0!c1!) ELSE (SET number=!c1!) SET text=%%A SET old=%before:$#=!number!% SET c2=1 FOR /F "usebackq delims=" %%B IN (`dir /b !old!`) DO ( IF !c1! == !c2! SET old="%%B" SET /A c2+=1 ) FOR /F %%B IN (!old!) DO ( SET old_name=%%~nB SET old_ext=%%~xB SET old_ext=!old_ext:~1! ) SET new=%after:$#=!number!% FOR /F "delims=" %%B IN ("!text!") DO SET new=!new:$L=%%B! FOR /F "delims=" %%B IN ("!old_name!") DO SET new=!new:$N=%%B! FOR /F "delims=" %%B IN ("!old_ext!") DO SET new=!new:$X=%%B! IF EXIST !old! (REN !old! !new!) SET /A c1 += 1 )