.bat for batch rename to increment numbers in fname

我与影子孤独终老i 提交于 2019-12-01 12:12:27
PA.

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.

:: REN-sec.cmd ==> Rename files to increment numbers
:: Inspired on -> http://stackoverflow.com/questions/6322329#6324312
:: - (cX) 2017 adolfo.dimare@gmail.com
:: -> 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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!