在特定字符后重命名文件
Renaming files after a specific character
希望在这里得到一些帮助。我有一堆文件被监控并删除了结束设施,文件如下所示:
姓氏,FirstName_DOS-Facility.pdf
我目前运行以下:
@echo off
for /F "tokens=1,* delims=-" %%a in ('dir /A-D /B "*.pdf"') do (
ECHO move "%%a-%%b" "%%a%%~xb"
)
并创建 LastName,FirstName_DOS.pdf
我运行遇到的问题是多个同名文件,我的批处理文件只是用新文件替换旧文件。有没有办法在需要时附加 _1.pdf _2.pdf _3.pdf 等?感谢您的帮助!
这是适合您的任务要求的批处理代码。
@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /F "tokens=1* delims=-" %%a in ('dir "*-*.pdf" /A-D /B 2^>nul') do (
if not exist "%%a%%~xb" (
ren "%%a-%%b" "%%a%%~xb"
) else (
call :GetNextAppendix "%%a" "%%~xb"
ren "%%a-%%b" "%%a!NextAppendix!%%~xb"
)
)
endlocal
goto :EOF
rem This subroutine inserts between file name passed as first argument
rem and file extension passed as second argument an underscore and an
rem incrementing number in range 1 to 50000 and checks if a file with
rem this name already exists. If there is no file with current number
rem in file name, this number with the preceding underscore is assigned
rem to an environment variable used in parent process routine to rename
rem the file.
:GetNextAppendix
for /L %%I in (1,1,50000) do (
if not exist "%~1_%%I%~2" (
set "NextAppendix=_%%I"
goto :EOF
)
)
rem Windows command interpreter should never reach this part of the code.
rem But in case of this really happens, simply clear the appendix variable
rem which results in a failed renaming of file in parent process loop
rem above with output of an error message because of file already existing.
set "NextAppendix="
goto :EOF
要了解使用的命令及其工作原理,请打开命令提示符 window,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。
call /?
echo /?
endlocal /?
for /?
goto /?
if /?
ren /?
set /?
setlocal /?
另请参阅有关 Using command redirection operators 的 Microsoft 文章以了解 2^>nul
的解释,即 2>nul
与重定向运算符 >
转义为 ^
应用于执行命令 DIR 并且不被解释为命令 FOR 在命令行中无效位置的重定向操作。
这会将 DIR 在没有匹配通配符模式 *-*.pdf
的文件上从 STDERR 输出的错误消息重定向到设备 NUL 抑制它。
从打开的命令提示符将脚本保存到 test.bat 和 运行。用您的路径替换 dir 值。如果有任何错误,请告诉我。
@echo off
setlocal enabledelayedexpansion
set "dir=C:\My_Files"
pushd "%dir%"
for /F "tokens=1,* delims=-" %%a in ('dir /A-D /B "*.pdf" 2^>nul') do (
call :rename %%a %%b )
popd
exit /b
:rename
set "i="
:loop
if exist "%1!i!%~x2" (set /a "i+=1" & goto :loop)
ren "%1-%2" "%1!i!%~x2"
exit /b
希望在这里得到一些帮助。我有一堆文件被监控并删除了结束设施,文件如下所示:
姓氏,FirstName_DOS-Facility.pdf
我目前运行以下:
@echo off
for /F "tokens=1,* delims=-" %%a in ('dir /A-D /B "*.pdf"') do (
ECHO move "%%a-%%b" "%%a%%~xb"
)
并创建 LastName,FirstName_DOS.pdf
我运行遇到的问题是多个同名文件,我的批处理文件只是用新文件替换旧文件。有没有办法在需要时附加 _1.pdf _2.pdf _3.pdf 等?感谢您的帮助!
这是适合您的任务要求的批处理代码。
@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /F "tokens=1* delims=-" %%a in ('dir "*-*.pdf" /A-D /B 2^>nul') do (
if not exist "%%a%%~xb" (
ren "%%a-%%b" "%%a%%~xb"
) else (
call :GetNextAppendix "%%a" "%%~xb"
ren "%%a-%%b" "%%a!NextAppendix!%%~xb"
)
)
endlocal
goto :EOF
rem This subroutine inserts between file name passed as first argument
rem and file extension passed as second argument an underscore and an
rem incrementing number in range 1 to 50000 and checks if a file with
rem this name already exists. If there is no file with current number
rem in file name, this number with the preceding underscore is assigned
rem to an environment variable used in parent process routine to rename
rem the file.
:GetNextAppendix
for /L %%I in (1,1,50000) do (
if not exist "%~1_%%I%~2" (
set "NextAppendix=_%%I"
goto :EOF
)
)
rem Windows command interpreter should never reach this part of the code.
rem But in case of this really happens, simply clear the appendix variable
rem which results in a failed renaming of file in parent process loop
rem above with output of an error message because of file already existing.
set "NextAppendix="
goto :EOF
要了解使用的命令及其工作原理,请打开命令提示符 window,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。
call /?
echo /?
endlocal /?
for /?
goto /?
if /?
ren /?
set /?
setlocal /?
另请参阅有关 Using command redirection operators 的 Microsoft 文章以了解 2^>nul
的解释,即 2>nul
与重定向运算符 >
转义为 ^
应用于执行命令 DIR 并且不被解释为命令 FOR 在命令行中无效位置的重定向操作。
这会将 DIR 在没有匹配通配符模式 *-*.pdf
的文件上从 STDERR 输出的错误消息重定向到设备 NUL 抑制它。
从打开的命令提示符将脚本保存到 test.bat 和 运行。用您的路径替换 dir 值。如果有任何错误,请告诉我。
@echo off
setlocal enabledelayedexpansion
set "dir=C:\My_Files"
pushd "%dir%"
for /F "tokens=1,* delims=-" %%a in ('dir /A-D /B "*.pdf" 2^>nul') do (
call :rename %%a %%b )
popd
exit /b
:rename
set "i="
:loop
if exist "%1!i!%~x2" (set /a "i+=1" & goto :loop)
ren "%1-%2" "%1!i!%~x2"
exit /b