.bat 用于批量重命名以增加 fname 中的数字

发布于 2024-11-14 20:29:29 字数 382 浏览 9 评论 0原文

我有一个很大的 .cbr 文件夹,我正在按问题号重命名它们以正确排序。我需要在 ren 行中包含什么,才能让每个文件通过 Windows 命令提示符增加文件名中的数字?我会经常这样做,因此我会将其设为 .bat 文件。

例如,其中 n = 初始数字,m = 最终数字: n.cbr, (n+1).cbr, ..., (m-1).cbr, m.cbr

到目前为止的 .bat:

ren *.cbz *.cbr
ren *.cbr <increment numbers n through m>.cbr

或者,如何做我修剪每个文件名,以便仅在扩展名之前留下数字? (从 issues1.cbr 到 1.cbr)通过 .bat 或脚本主机文件?

I have a large folder of .cbr's, and I'm renaming them by issue number to correctly order them. What do I need to include in the ren line to have each file increment the number in the file name via windows command prompt? I'll be doing this frequently so I'll make this a .bat file.

For example, where n = initial number and m = final number: n.cbr, (n+1).cbr, ..., (m-1).cbr, m.cbr

The .bat thusfar:

ren *.cbz *.cbr
ren *.cbr <increment numbers n through m>.cbr

Alternatively, how do I trim each file name so that only the numbers are left before the extension? (from issue1.cbr to 1.cbr) via either a .bat or script host file?

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

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

发布评论

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

评论(2

離人涙 2024-11-21 20:29:30

试试这个批处理脚本。

@echo off
setlocal enabledelayedexpansion
set /a count=0
for /f "tokens=*" %%a in ('dir /b /od *.cbr') do (
 echo ren "%%a" !count!.cbr
 set /a count+=1
)

它使用增量计数器重命名所有文件。文件的顺序由 DIR 命令的 /OD 选项保留,该选项按修改后的时间戳对文件列表进行排序。

经过仔细测试,删除 ECHO 命令。

有关详细信息,请阅读HELP DIRHELP SETHELP FOR

Try this batch script.

@echo off
setlocal enabledelayedexpansion
set /a count=0
for /f "tokens=*" %%a in ('dir /b /od *.cbr') do (
 echo ren "%%a" !count!.cbr
 set /a count+=1
)

It renames all the files with a incremental counter. The order of the files is preserved with the /OD option of the DIR command, that sorts the files list by its modified timestamp.

After careful testing, remove the ECHO command.

For more information, read HELP DIR, HELP SET and HELP FOR.

伴随着你 2024-11-21 20:29:30
:: REN-sec.cmd ==> Rename files to increment numbers
:: Inspired on -> http://stackoverflow.com/questions/6322329#6324312
:: - (cX) 2017 [email protected]
:: -> CAVEAT: Works on CURRENT directory!
:: - Tested on Win10 64bits
@echo off

if (%1)==()   goto _help
if (%1)==(/?) goto _example

setLOCAL EnableDelayedExpansion
rem            EnableExtensions
if (%1)==(/r) goto _recursive
if (%1)==(/R) goto _recursive
goto _current_dir

:_recursive
for /d %%A in (*.*) do (
      cd "%%~A"
      call %~dpnx0 %1 %2 %3 %4
      rem echo returning -^> call %~dpnx0 %1 %2 %3 %4
      cd ..
)
shift
goto _current_dir

:_current_dir
set /a _count=0
for %%A in (%1) do (
    set /a _count+=1

    rem Execute several times to rename 'crazy' left overs...
    FOR /L %%C IN (1,1,2) DO (
        if exist "%~2!_count!%~3%%~xA" call :skip_count %1 %2 %3 %AA
        if exist "%%~A" if not exist "%~2!_count!%~3%%~xA" ren "%%~A" "%~2!_count!%~3%%~xA"
    )
    if exist "%%~A" echo EXISTS "%%~A"
    REM if not exist "%~2!_count!%~3%%~xA" echo MISSING %~2!_count!%~3%%~xA
)
goto _out

:skip_count
    set /a _count+=1
    if exist "%~2!_count!%~3%%~x4" goto skip_count
goto _out

:_example
echo.
echo %0 USAGE EXAMPLE
echo.
echo X:\Dir\SubDir\^> dir /b
echo etc.
echo La La La.mp3
echo Le Le Le.mp3
echo Lo Lo Lo.mp3
echo Other.txt
echo.
echo X:\Dir\SubDir\^> %0 *.mp3 "" " - Su Fix"
echo.
echo X:\Dir\SubDir\^> dir /b
echo etc.
echo 1 - Su Fix.mp3
echo 2 - Su Fix.mp3
echo 3 - Su Fix.mp3
echo Other.txt
echo.

:_help
echo Rename files to increment numbers
echo.
echo CAVEAT: Works only in CURRENT directory!
echo.
echo %0 [/r] *.ext [prefix] [sufix]
echo:  /?          Help screen
echo   /r          Recurse in directories
echo   *.ext       Simple wildcard (like *.mp3) [NOT *.a.txt]
echo   prefix      Prefix to rename number
echo   sufix       sufix to rename number
echo.
echo When "" is used as prefix no prefix is put before the increment number
echo.
echo X:\Dir\SubDir\^> %0 [/r] [wildcard] [prefix] [sufix]
goto _out

:_out
:: REN-sec.cmd ==> End of file
:: REN-sec.cmd ==> Rename files to increment numbers
:: Inspired on -> http://stackoverflow.com/questions/6322329#6324312
:: - (cX) 2017 [email protected]
:: -> CAVEAT: Works on CURRENT directory!
:: - Tested on Win10 64bits
@echo off

if (%1)==()   goto _help
if (%1)==(/?) goto _example

setLOCAL EnableDelayedExpansion
rem            EnableExtensions
if (%1)==(/r) goto _recursive
if (%1)==(/R) goto _recursive
goto _current_dir

:_recursive
for /d %%A in (*.*) do (
      cd "%%~A"
      call %~dpnx0 %1 %2 %3 %4
      rem echo returning -^> call %~dpnx0 %1 %2 %3 %4
      cd ..
)
shift
goto _current_dir

:_current_dir
set /a _count=0
for %%A in (%1) do (
    set /a _count+=1

    rem Execute several times to rename 'crazy' left overs...
    FOR /L %%C IN (1,1,2) DO (
        if exist "%~2!_count!%~3%%~xA" call :skip_count %1 %2 %3 %AA
        if exist "%%~A" if not exist "%~2!_count!%~3%%~xA" ren "%%~A" "%~2!_count!%~3%%~xA"
    )
    if exist "%%~A" echo EXISTS "%%~A"
    REM if not exist "%~2!_count!%~3%%~xA" echo MISSING %~2!_count!%~3%%~xA
)
goto _out

:skip_count
    set /a _count+=1
    if exist "%~2!_count!%~3%%~x4" goto skip_count
goto _out

:_example
echo.
echo %0 USAGE EXAMPLE
echo.
echo X:\Dir\SubDir\^> dir /b
echo etc.
echo La La La.mp3
echo Le Le Le.mp3
echo Lo Lo Lo.mp3
echo Other.txt
echo.
echo X:\Dir\SubDir\^> %0 *.mp3 "" " - Su Fix"
echo.
echo X:\Dir\SubDir\^> dir /b
echo etc.
echo 1 - Su Fix.mp3
echo 2 - Su Fix.mp3
echo 3 - Su Fix.mp3
echo Other.txt
echo.

:_help
echo Rename files to increment numbers
echo.
echo CAVEAT: Works only in CURRENT directory!
echo.
echo %0 [/r] *.ext [prefix] [sufix]
echo:  /?          Help screen
echo   /r          Recurse in directories
echo   *.ext       Simple wildcard (like *.mp3) [NOT *.a.txt]
echo   prefix      Prefix to rename number
echo   sufix       sufix to rename number
echo.
echo When "" is used as prefix no prefix is put before the increment number
echo.
echo X:\Dir\SubDir\^> %0 [/r] [wildcard] [prefix] [sufix]
goto _out

:_out
:: REN-sec.cmd ==> End of file
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文