views:

2002

answers:

2

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.

+1  A: 

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.

Joey
A: 

Tom, fiddling around with a previous script I posted, here is one processing all files and subdirectories:

recurename.cmd directory

@echo off
setlocal ENABLEDELAYEDEXPANSION
set Replaces="V=Y" "W=Z" "m=A"
set StartDir=%~dp1
call :RenameDirs "%StartDir:~0,-1%"
exit /b 0
:RenameDirs
    call :RenameFiles "%~1"
    for /f "delims=" %%d in ('dir /ad /b "%~1"2^>nul') do call :RenameDirs "%~1\%%~d"
    if "%~1"=="%StartDir:~0,-1%" exit /b 0
    set _pos=0
    set _dir=%~f1
    set _dir_orig=!_dir!
    :finddirName
     set /a _pos-=1
     call set _dir_pos=!!!_dir:~%_pos%,1!!!
     if not "%_dir_pos%"=="\" goto finddirName
     call set _origines=!!!_dir:~0,%_pos%!!!
     set /a _pos+=1
     call set _dir=!!!_dir:~%_pos%!!!
    for %%r in (%Replaces%) do call set _dir=!!!_dir:%%~r!!!
    if /i not "!_dir!"=="!_dir_orig!" echo move "%~1" "%_origines%\!_dir!"
    exit /b 0
:RenameFiles
    for /f "delims=" %%f in ('dir /a-d /b "%~1"2^>nul') do (
     set _file=%%~nf
     set _file_orig=!_file!
     for %%r in (%Replaces%) do call set _file=!!!_file:%%~r!!!
     if /i not "!_file!"=="!_file_orig!" echo ren "%~1\%%f" "%~1\!_file!%%~xf"
    )
    exit /b 0

___Notes____
This is a non-destructive script, remove the echo from the correct commands in order to rename any file/directory. (echo move and echo ren)

Set Replaces=: Set this variable to whatever pairs you need changed.

set Startdir=: I wanted to secure, somehow, the argument and taking only the path from it. If a files is given as a parameter, the whole container directory and subdirs will be processed.

if "%~1"=="%StartDir:~0,-1%" exit /b 0: This line was placed to stop the argument directory itself from being processed. If you wish so, remove this line.
If the script is called with, say, *c:\temp\*, removing this line would change the name to *c:\teAp\* in the end.

Jay