Rename all files with a sequence number

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
kg2uin
Posts: 4
Joined: 04 Feb 2011 01:49

Rename all files with a sequence number

#1 Post by kg2uin » 04 Feb 2011 01:57

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.

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

Re: Rename all files with a sequence number

#2 Post by aGerman » 04 Feb 2011 09:15

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

kg2uin
Posts: 4
Joined: 04 Feb 2011 01:49

Re: Rename all files with a sequence number

#3 Post by kg2uin » 04 Feb 2011 15:31

Thanks a lot ..... Works well.

kg2uin
Posts: 4
Joined: 04 Feb 2011 01:49

Re: Rename all files with a sequence number

#4 Post by kg2uin » 04 Feb 2011 20:47

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.

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

Re: Rename all files with a sequence number

#5 Post by aGerman » 05 Feb 2011 07:31

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]

kg2uin
Posts: 4
Joined: 04 Feb 2011 01:49

Re: Rename all files with a sequence number

#6 Post by kg2uin » 05 Feb 2011 13:49

Thanks a lot ..... works like a slice of cake.

Werner
Posts: 1
Joined: 15 Nov 2012 13:37

Re: Rename all files with a sequence number

#7 Post by Werner » 15 Nov 2012 13:40

Hi Guys,

Thanks for this post, i would like the same but without space, can you please help?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Rename all files with a sequence number

#8 Post by foxidrive » 15 Nov 2012 17:33

What do you mean? Is there a space?

redclawkefar
Posts: 13
Joined: 20 Nov 2012 16:04

Re: Rename all files with a sequence number

#9 Post by redclawkefar » 21 Nov 2012 18:14

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.

Post Reply