Rename all files with a sequence number
Moderator: DosItHelp
Rename all files with a sequence number
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.
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
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:
Regards
aGerman
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
Thanks a lot ..... Works well.
Re: Rename all files with a sequence number
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.
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
It's the same like before.
Regards
aGerman
[EDIT] Option e replaced by x [/EDIT]
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
Thanks a lot ..... works like a slice of cake.
Re: Rename all files with a sequence number
Hi Guys,
Thanks for this post, i would like the same but without space, can you please help?
Thanks for this post, i would like the same but without space, can you please help?
Re: Rename all files with a sequence number
What do you mean? Is there a space?
-
- Posts: 13
- Joined: 20 Nov 2012 16:04
Re: Rename all files with a sequence number
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.
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.