0
  • Fullcast 01 [3m].jpg
  • Fullcast 01 [5m].jpg
  • KingPin - 02 [3m].jpg
  • KingPin - 02 [5m].jpg
  • ThemaCast - 8745 [3m].png
  • ThemaCast - 8745 [5m].png

I have multiple groups of files [group - Fullcast,KingPin,etc...] which follow the above pattern .I want to replace the strings [3m] with BZC and [5m] with HZC in all those files and also replace the strings "Fullcast" with "Fllcst" ; "KingPin" with "KngPn" ; "Themecast" with "ThemaCst" and so on......... using a single bat file.

I got answer but only for a single string - How to rename file by replacing substring using batch in Windows

I can create multiple bat files but How to rename those using a single bat file ? Note : the files are in "D:\Images\" and i want to launch the bat file from Desktop [C:\Documents and Settings\XXX\Desktop]

Community
  • 1
  • 1
Martin
  • 31
  • 1
  • 2
  • 10

1 Answers1

0

You didn't say how your script was going to get the list of files on which to operate. This example assumes that you're looking at all the files in the current directory.

@ECHO OFF
SETLOCAL 

FOR %%i IN (*.jpg) DO CALL :CUSTOM_RENAME "%%~i"
FOR %%i IN (*.png) DO CALL :CUSTOM_RENAME "%%~i"

EXIT /B

:CUSTOM_RENAME
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "delims=*" %%f IN ("%~1") DO (
    SET "SOURCE=%%~f"
    SET "NEWNAME=%%~nxf"
)
SET "NEWNAME=!NEWNAME:[3m]=BZC!"
SET "NEWNAME=!NEWNAME:[5m]=HZC!"
REM Other substitutions would go here, assuming they don't overlap.

@ECHO "!SOURCE!" --^> "!NEWNAME!"
rename "!SOURCE!" "!NEWNAME!"
ENDLOCAL
EXIT /B
mojo
  • 4,050
  • 17
  • 24