txt file renaming

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

txt file renaming

#1 Post by joe » 09 Dec 2017 09:18

hi

I have hundreds of text files in a folder every morning they are sequentially renamed ie 2017120100001.txt (yyyymmdddigit.txt) and
moved to another pc.
my request is
is there a way to sequentially rename these text files and make the batch file save the last seq number
and continue to to rename if its run the next time (ie first run 00001 - 00100, second run 00101 - 00190) and so on

pl help
thanx in advance

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: txt file renaming

#2 Post by Squashman » 09 Dec 2017 10:36

Going to assume you have some existing code you are already using that you could show us.

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

Re: txt file renaming

#3 Post by joe » 09 Dec 2017 19:04

hi
right now its manually done which is really time consuming

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

Re: txt file renaming

#4 Post by joe » 09 Dec 2017 19:50

steffen did give me a script which worked well and was useful but the old data had to be retained
in the directory for the script to work.

the script is





@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
)




but now the data of the previous day is deleted and the script starts all over again from 00001 .

I need to continue with a new folder with .txt files.

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

Re: txt file renaming

#5 Post by joe » 09 Dec 2017 22:48

steffen did give me a script which worked well and was useful but the old data had to be retained
in the directory for the script to work.

the script is





@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
)




but now the data of the previous day is deleted and the script starts all over again from 00001 .

I need to continue with a new folder with .txt files.

Post Reply