Page 1 of 1
Rename all files with a sequence number
Posted: 04 Feb 2011 01:57
by kg2uin
Hi All,
I need to rename all files of a folder by appending a 4 digit sequence number to it.
Eg. if my folder has 3 files namely A.txt, B.txt and C.txt then i want to rename them to A0001.txt, B0002.txt and C0003.txt
I want to achieve the same through a DOS batch script
Please help me with suitable answers.
Thanks in advance.
Re: Rename all files with a sequence number
Posted: 04 Feb 2011 09:15
by aGerman
If you run the renaming the first time every thing should be all right. But what happens if run it twice?
You probably need a code that checks all text files for a sequence no., finds the greatest and starts with next higher. On the other hand it should ignore all files with a sequence no. during the rename-loop. It's a bit complicated.
Hope this will work for you:
Code: Select all
@echo off
setlocal enabledelayedexpansion
set "index=0000"
for /f "delims=" %%a in ('dir /a-d /b *.txt^|findstr /rec:".[0-9][0-9][0-9][0-9]\.txt"') do (
set "name=%%~na"
if "!name:~-4!" gtr "!index!" set "index=!name:~-4!"
)
set /a i=1%index%
for /f "delims=" %%a in ('dir /a-d /b *.txt^|findstr /revc:".[0-9][0-9][0-9][0-9]\.txt"') do (
set /a i+=1
ren "%%~a" "%%~na!i:~-4!%%~xa"
)
Regards
aGerman
Re: Rename all files with a sequence number
Posted: 04 Feb 2011 15:31
by kg2uin
Thanks a lot ..... Works well.
Re: Rename all files with a sequence number
Posted: 04 Feb 2011 20:47
by kg2uin
Hi Experts,
There is a small change to my requirement the file names would be:
D_NEW_String.txt
D_NEW_String2.txt
D_NEW_String123.txt
D_NEW_xyz.txt
And i want the output of renaming to be:
D_NEW_0001.txt
D_NEW_0002.txt
D_NEW_0003.txt
D_NEW_0004.txt
So, i have to retain the first 6 charcters of the file name and append a four character sequence number(xxxx) to it.
Please provide a solution for it.
Re: Rename all files with a sequence number
Posted: 05 Feb 2011 07:31
by aGerman
It's the same like before.
Code: Select all
@echo off
setlocal enabledelayedexpansion
set "index=0000"
for /f "delims=" %%a in ('dir /a-d /b *.txt^|findstr /rxc:"......[0-9][0-9][0-9][0-9]\.txt"') do (
set "name=%%~na"
if "!name:~-4!" gtr "!index!" set "index=!name:~-4!"
)
set /a i=1%index%
for /f "delims=" %%a in ('dir /a-d /b *.txt^|findstr /rxvc:"......[0-9][0-9][0-9][0-9]\.txt"') do (
set /a i+=1
set "name=%%~na"
ren "%%~a" "!name:~,6!!i:~-4!%%~xa"
)
Regards
aGerman
[EDIT] Option e replaced by x [/EDIT]
Re: Rename all files with a sequence number
Posted: 05 Feb 2011 13:49
by kg2uin
Thanks a lot ..... works like a slice of cake.
Re: Rename all files with a sequence number
Posted: 15 Nov 2012 13:40
by Werner
Hi Guys,
Thanks for this post, i would like the same but without space, can you please help?
Re: Rename all files with a sequence number
Posted: 15 Nov 2012 17:33
by foxidrive
What do you mean? Is there a space?
Re: Rename all files with a sequence number
Posted: 21 Nov 2012 18:14
by redclawkefar
if you really want o do mass renaming of files with exchanging and adding sequences and text to file names there is a really nice simple utility i found and use. if you ave $20us to spare its a great find.
take a look at "better file rename" :
http://www.publicspace.net/windows/BetterFileRename/no, i'm not an owner and i won't get any benefits for posting the link but i have run into the rename issue hundreds of times and found it VERY useful. thought i'd just share it though.