sequential rename

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
joe
Posts: 35
Joined: 06 Sep 2017 07:56

sequential rename

#1 Post by joe » 06 Sep 2017 08:07

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..

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: sequential rename

#2 Post by pieh-ejdsch » 06 Sep 2017 10:09

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:

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: sequential rename

#3 Post by aGerman » 06 Sep 2017 10:15

I agree with Phil. A Batch script doesn't have a brain to remember the last number :lol:

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

joe
Posts: 35
Joined: 06 Sep 2017 07:56

Re: sequential rename

#4 Post by joe » 07 Sep 2017 07:01

aGerman wrote:I agree with Phil. A Batch script doesn't have a brain to remember the last number :lol:

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






thank you phil and steffen for giving me the right code it worked perfectly easing my work

Post Reply