使用批处理文件递归重命名文件名+文件夹名称

时间:2009-04-04 14:20:24

标签: batch-file rename batch-rename

我喜欢创建一个批处理文件(winxp cmd),它递归遍历一个已选择的文件夹和子文件夹,并使用以下规则重命名文件+文件夹:

从所有文件+文件夹名称,所有大写+小写“V”和“W”字母需要替换为字母“Y”和“Z”。

e.g。 11V0W必须变为11Y0Z。

我相信FOR / R有可能但是怎么样? 我认为除了使用FOR / R进行基本递归之外,还需要调用一个子程序来逐个检查每个字母。

3 个答案:

答案 0 :(得分:6)

以下批处理至少对文件名执行此操作。目录有点棘手(至少到目前为止我无法提出非无限的解决方案):

@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

但从理论上讲,将dir重命名放在这上面应该不会太难。

答案 1 :(得分:6)

汤姆,摆弄我发布的前一个脚本,这里有一个处理所有文件和子目录:

recurename.cmd 目录

@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

<强> ___注释____
这是一个非破坏性脚本,从正确的命令中删除 echo 以重命名任何文件/目录。 ( echo move echo ren

设置替换=:将此变量设置为您需要更改的任何对。

设置Startdir =:我想以某种方式保护参数并仅从中获取路径。如果文件作为参数给出,则将处理整个容器目录和子目录。

如果“%~1”==“%StartDir:~0,-1%”退出/ b 0:此行用于停止处理参数目录本身。如果您愿意,请删除此行 如果使用 c:\ temp \ 调用脚本,则删除此行会最终将名称更改为 c:\ teAp \

答案 2 :(得分:0)

使用此目录树:

.
├── 00v0w
│   ├── 12V0W
│   ├── 12d0w
│   └── 12v0d
├── 11V0W
├── 11d0w
└── 11v0d

这些renamer命令:

$ renamer --regex --find '[vV]' --replace 'Y' '**'
$ renamer --regex --find '[wW]' --replace 'Z' '**'

会产生这个输出:

.
├── 00Y0Z
│   ├── 12Y0Z
│   ├── 12Y0d
│   └── 12d0Z
├── 11Y0Z
├── 11Y0d
└── 11d0Z
相关问题