递归重命名文件名 + 带有批处理文件的文件夹名称

发布于 2024-07-16 13:51:24 字数 230 浏览 8 评论 0原文

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

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

例如,11V0W 必须变为 11Y0Z。

我相信 FOR /R 是可能的,但是如何呢? 我认为除了 FOR /R 的基本递归之外,还需要调用一个子例程来逐一检查每个字母。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

此岸叶落 2024-07-23 13:51:24

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

@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

但从理论上讲,将目录重命名附加到此上应该不会太难。

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.

两仪 2024-07-23 13:51:24

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

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

___Notes____
这是一个非破坏性脚本,请从正确的命令中删除回显,以便重命名任何文件/目录。 (echo moveecho ren)

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

set Startdir=: 我想以某种方式保护该参数并仅采用其中的路径。 如果将文件作为参数给出,则将处理整个容器目录和子目录。

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

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.

傲影 2024-07-23 13:51:24

使用此目录树:

.
├── 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

with this directory tree:

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

these renamer commands:

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

would produce this output:

.
├── 00Y0Z
│   ├── 12Y0Z
│   ├── 12Y0d
│   └── 12d0Z
├── 11Y0Z
├── 11Y0d
└── 11d0Z
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文