Friday, October 4, 2013

Renaming your recursive files and folder using a batch file.

Initially I was using a 3rd party software to do all the renaming work. But as I proceed, I ended up searching high and low for a method using a batch file (*.bat) to do all the tedious work. Therefore, after playing and modifying the code, I have ended up with this.

@echo off
setlocal enabledelayedexpansion
REM for renaming of recursive folders, sub folders
for /d /r %%f in (*.*) do (
set fn=%%~nxf

REM replace all the . with __ in the folder names (excludes extensions)
if not [!fn!]==[] ( 
if not ["%%~nxf"]==["!fn:.=__!"] ( 
echo ren "%%~f" "!fn:.=__!" 
ren "%%~f" "!fn:.=__!" 
)
)
REM replace all the -interface with nothing in the folder names
REM (excludes extensions) add a I in front of the folder name
if not [!fn!]==[] ( 
if not ["%%~nxf"]==["!fn:-interface=!"] ( 
echo ren "%%~f" "I!fn:-interface=!" 
ren "%%~f" "I!fn:-interface=!" 
)
)
)

REM for renaming of recursive files
for /r %%f in (*.*) do (
set fn=%%~nf

REM replace all the - with __ in the file names (excludes extensions)
if not [!fn!]==[] ( 
if not ["%%~nxf"]==["!fn:-=__!%%~xf"] ( 
echo ren "%%~f" "!fn:-=__!%%~xf" 
ren "%%~f" "!fn:-=__!%%~xf" 
)
)
REM replace all the -interface with nothing in the file names
REM (excludes extensions) add a I in front of the file name
if not [!fn!]==[] ( 
if not ["%%~nxf"]==["!fn:-interface=!%%~xf"] ( 
echo ren "%%~f" "I!fn:-interface=!%%~xf" 
ren "%%~f" "I!fn:-interface=!%%~xf" 
)
)
)
pause

* Click here to download the batch file that I have listed in this posted.
^ Click here to find out more about batch files in Windows.
~ Click here to find out the difference between %% and !! in a batch file.

No comments:

Post a Comment