@echo off REM %1 Filename prefix: 'test' in 'test01.dat' REM %2 Filename extension: '.dat' in 'test01.dat' (default: .dat) REM %3 Number of digits in target filename (default: 3) REM Valid values are 2, 3 and 4. REM REM Renames files in a numerical sequence, inserting leading zeroes to REM give the sequence numbers the same number of digits, e.g. REM If the sequence is test1.dat, .., test9.dat, test10.dat, ..., test99.dat, REM test100.dat, etc., then REM fixname test 4 REM will rename the files to create the sequence REM test0001.dat, .., test0010.dat, test0100.dat, etc. REM REM Calls: REM makelist.bat REM Restrictions: REM The location of fixname.bat (and makelist.bat) should be in the REM path to allow for recursive calls. REM Check for recursive calls if "%1" == "changing" goto change if "%1" == "looping3" goto loopy3 if "%1" == "looping4" goto loopy4 if "%1" == "looping5" goto loopy5 if "%1" == "looping6" goto loopy6 REM Check for prefix. If not present then stop. If present store in env var if "%1" == "" goto no_prefix set pre=%1 set ext=%2 if "%ext%" == "" set ext=.dat set len=%3 if "%len%" == "" set len=3 if "%len%" == "1" goto exit if "%len%" == "2" goto two_dig if "%len%" == "3" goto three_dig if "%len%" == "4" goto four_dig REM ============================ :two_dig REM Process list (0.img .... 9.img) call makelist no_prefix %ext% for %%i in %list% do call fixname changing 1 %%i set list= goto cleanup REM ============================ :three_dig REM Process list (0.img .... 9.img) call makelist no_prefix %ext% for %%i in %list% do call fixname changing 2 %%i set list= REM Process list (0 1 ... 9) call makelist no_prefix set longlist=%list% for %%i in %longlist% do call fixname looping3 %%i set longlist= goto cleanup REM ------------------------ :loopy3 set tmparg=%2% call makelist %tmparg% %ext% for %%j in %list% do call fixname changing 1 %%j set list= set tmparg= goto exit REM ============================ :four_dig REM Process list (0.img .... 9.img) call makelist no_prefix %ext% for %%i in %list% do call fixname changing 3 %%i set list= REM Process list (0 1 ... 9) call makelist no_prefix set longlist=%list% for %%i in %longlist% do call fixname looping4 %%i set longlist= call makelist no_prefix set longlist=%list% for %%i in %longlist% do call fixname looping5 %%i set longlist= goto cleanup REM ------------------------ :loopy4 set tmparg=%2% call makelist %tmparg% %ext% for %%j in %list% do call fixname changing 2 %%j set list= set tmparg= goto exit REM ============================ :loopy5 call makelist %2% set list5=%list% for %%j in %list5% do call fixname looping6 %%j set list5= goto exit REM ============================ :loopy6 set tmparg=%2% call makelist %tmparg% %ext% for %%k in %list% do call fixname changing 1 %%k set list= set tmparg= goto exit REM ============================ :change if "%2" == "1" goto one_z if "%2" == "2" goto two_z if "%2" == "3" goto three_z goto exit :one_z set zeroes=0 goto change_z :two_z set zeroes=00 goto change_z :three_z set zeroes=000 goto change_z :change_z set from=%pre%%3% if not exist %from% goto exit set to=%pre%%zeroes%%3% REM @echo on ren %from% %to% echo %from% --- %to% REM @echo off goto exit REM Cleanup routine: delete environment variables :cleanup set to= set from= set zeroes= rem set list= set pre= set len= goto exit REM Error exit if no prefix is specified :no_prefix echo Error: echo Error: Specify prefix as first argument goto exit :exit