windows - Increment a number with leading zeros by a batch file -
getting trouble incrementing number zeros in front.
it fail after 0008 telling me filename exist or file not found. odd.
here script:
@echo off copy nul %tmp%\filename.tmp /f "tokens=2 delims=-." %%a in ( 'dir /b "%tmp%\filename*.tmp" ^|sort /r ^|findstr /r [0-9]' ) ( set "num=%%a" goto :done ) ren %tmp%\filename.tmp filename-0001.tmp exit /b 0 :done echo highest number found %num% set /a num+=1 set incr=0000%num% ren %tmp%\filename.tmp filename-%incr:~-4%.tmp dir /b "%tmp%\filename*.tmp" |sort /r thus simplifying problem:
@echo off set "num=008" echo num: %num% set /a num=num+1 echo num: %num% echo let's try syntax set "num=008" echo num: %num% set /a num+=1 echo num: %num% output:
num: 008 num: 1 let's try syntax num: 008 num: 1
another way add preceding 1 (so it's not interpreted octal), increment, trim away 1.
the largest number limited cmd's set /a command.
@echo off set "num=008" echo num: %num% set "num=1%num%" echo num: %num% set /a num=num+1 echo num: %num% set num=%num:~1% echo num: %num% pause
Comments
Post a Comment