iam new to coding and have no knowledge in batch scripting.
my request is
a batch script to rename .txt files sequentially but remember the last count
to continue renaming when the script is run next time.
ie
file0000001.txt - file0000999.txt (first session)
file0001000.txt - file0002000.txt (second session)
any help on this would be grateful.
thanks in advance..
sequential rename
Moderator: DosItHelp
-
- Posts: 240
- Joined: 04 Mar 2014 11:14
- Location: germany
Re: sequential rename
Hello joe,
start at the end a subroutine which enters the number into the batch. This subroutine must be at the bottom.
write this to your batch, leaving the last blank line at the end without line break:
thus always updated automatically. and you do not need another file.
Try with a copy of your batch!
Phil
start at the end a subroutine which enters the number into the batch. This subroutine must be at the bottom.
write this to your batch, leaving the last blank line at the end without line break:
Code: Select all
@echo off
setlocal
call :continueCount
rem your Script
set /a countNR+=1
echo %countNR%
rem END your Script
for /f "delims=" %%i in ('findstr $ "%~f0" ^&type nul ^>"%~f0"') do @ >>"%~f0" echo %%i
<nul >>"%~f0" set /p "=set /a countNR=+%countNR%"
pause
exit /b
:continueCount
rem Begin with this Counter - next Line without LineFeed
set /a countNR=+5
thus always updated automatically. and you do not need another file.
Try with a copy of your batch!
Phil
Re: sequential rename
I agree with Phil. A Batch script doesn't have a brain to remember the last number
While Phil posted his script I wrote another one
Steffen
While Phil posted his script I wrote another one
Code: Select all
@echo off &setlocal
set "basename=file"
:: determine the greatest number
set "greatest=10000000"
setlocal EnableDelayedExpansion
for /f "delims=" %%i in ('dir /a-d /b *.txt^|findstr /xi "!basename![0-9][0-9][0-9][0-9][0-9][0-9][0-9]\.txt"') do (
set "name=%%~ni"
set /a "current=1!name:~-7!"
if !current! gtr !greatest! set /a "greatest=current"
)
endlocal &set "greatest=%greatest%"
:: rename new files
for /f "delims=" %%i in ('dir /a-d /b *.txt^|findstr /vxi "%basename%[0-9][0-9][0-9][0-9][0-9][0-9][0-9]\.txt"') do (
set "file=%%i"
set /a "greatest+=1"
setlocal EnableDelayedExpansion
ren "!file!" "!basename!!greatest:~-7!.txt"
endlocal
)
Steffen
Re: sequential rename
aGerman wrote:I agree with Phil. A Batch script doesn't have a brain to remember the last number
While Phil posted his script I wrote another oneCode: Select all
@echo off &setlocal
set "basename=file"
:: determine the greatest number
set "greatest=10000000"
setlocal EnableDelayedExpansion
for /f "delims=" %%i in ('dir /a-d /b *.txt^|findstr /xi "!basename![0-9][0-9][0-9][0-9][0-9][0-9][0-9]\.txt"') do (
set "name=%%~ni"
set /a "current=1!name:~-7!"
if !current! gtr !greatest! set /a "greatest=current"
)
endlocal &set "greatest=%greatest%"
:: rename new files
for /f "delims=" %%i in ('dir /a-d /b *.txt^|findstr /vxi "%basename%[0-9][0-9][0-9][0-9][0-9][0-9][0-9]\.txt"') do (
set "file=%%i"
set /a "greatest+=1"
setlocal EnableDelayedExpansion
ren "!file!" "!basename!!greatest:~-7!.txt"
endlocal
)
Steffen
thank you phil and steffen for giving me the right code it worked perfectly easing my work