recursive renaming file names + folder names with a batch file

Tom picture Tom · Apr 4, 2009 · Viewed 20.5k times · Source

I like to create a batch file (winxp cmd) that recursively goes through a chose folder and sub folders and renames there files+folders with the following rules:

from all file + folder names, all uppercase+lowercase "V" and "W" letters need to be replaced with letters "Y" and "Z".

e.g. 11V0W must become 11Y0Z.

I believe its possible with FOR /R but how? I think there needs to be a subroutine to be called that checks each letter one by one in addition to basic recursion with FOR /R.

Answer

Joey picture Joey · Apr 4, 2009

The following batch does this for the file names at least. Directories are a bit trickier (at least I couldn't come up with a non-infinite solution so far):

@echo off
setlocal enableextensions
for /r %%f in (*) do call :process "%%f"
endlocal
goto :eof

:process
pushd "%~dp1"
set fn=%~nx1
set fn=%fn:V=Y%
set fn=%fn:W=Z%
ren "%~nx1" "%fn%"
popd
goto :eof

But in theory it shouldn't be too hard to tack dir renaming onto this.