2011-06-12 122 views
1

我有一个大型的.cbr文件夹,并且我正在通过问题编号对它们进行重命名以正确排序它们。我需要在ren行中包含哪些内容才能让每个文件通过windows命令提示符递增文件名中的数字?我会经常这样做,所以我会把它做成一个.bat文件。.bat用于批量重命名以增加fname中的数字

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

的蝙蝠thusfar:

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

另外,我该如何调整每个文件名,以便只有数字扩展之前离开? (从issue1.cbr到1.cbr)通过.bat或脚本宿主文件?

+0

你说你把它们重命名为“......按问题编号......”脚本如何知道哪个文件应该有哪个编号?通过偷看到.cbr文件中的元数据?我认为你不会纯粹用批处理脚本来完成。 – 2011-06-12 13:58:01

+0

好的,如果我不打算每次改变.bat的话。首先,假设我每次想要指定一个新的范围n到m时编辑.bat。如果你有一个建议,找到文件组中的最小数量和最大数量,然后设置为n到m,让我知道(会更好,但我从来没有这样做过) – werdnanoslen 2011-06-12 14:02:31

+0

@werdnanoslen:但即使在批处理中,您不能保证Windows将处理'* .cbr'通配符的顺序,所以您将有效地在该范围内提供文件的随机名称。 – 2011-06-12 14:03:44

回答

6

试试这个批处理脚本。

@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 DIR,HELP SETHELP FOR

0
:: 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 
+0

CAVEAT:适用于CURRENT目录! – Adolfo 2017-05-20 17:50:15

+0

要更改文件扩展名,请使用http://stackoverflow.com/questions/17658354/#26438915 – Adolfo 2017-05-20 22:17:48

+0

错误:重命名结果将为1 3 5 7 9.您应该删除循环中的第一个“set/a _count + = 1”以防止增加两次。 – Eric 2018-01-22 04:41:11