Remove Sequence Nbr From Filename

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
marvlus
Posts: 3
Joined: 12 Mar 2012 12:16

Remove Sequence Nbr From Filename

#1 Post by marvlus » 12 Mar 2012 14:32

I have a list of PDF in a folder in which I need to remove the sequence number and the trailing space from each of the filenames. Example.

1 Account01.pdf
2 Account05.pdf
10 Account30.pdf

I want the end result to be:

Account01.pdf
Account05.pdf
Account30.pdf

Any help would be appreciated!

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

Re: Remove Sequence Nbr From Filename

#2 Post by foxidrive » 12 Mar 2012 14:44

Try this (untested) and remove the echo keyword if it looks right on the screen.
Make sure that only the files you want renamed are in the current folder.

Code: Select all

@echo off
for /f "tokens=1,*" %%a in ('dir /b *.pdf') do echo ren "%%a %%b" "%%b"

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

Re: Remove Sequence Nbr From Filename

#3 Post by aGerman » 12 Mar 2012 14:50

Code: Select all

@echo off
for /f "delims=" %%i in ('dir /a-d /b *pdf^|findstr /rbc:"[0-9][0-9]* "') do (
  for /f "tokens=1*" %%j in ("%%i") do ECHO ren "%%i" "%%k"
)
PAUSE

... to protect you from errors if there is more than one space between.
Also remove ECHO and PAUSE if it seems to work.

Regards
aGerman

marvlus
Posts: 3
Joined: 12 Mar 2012 12:16

Re: Remove Sequence Nbr From Filename

#4 Post by marvlus » 12 Mar 2012 14:58

foxidrive,
It worked! You're the one! Thanks very much!

marvlus
Posts: 3
Joined: 12 Mar 2012 12:16

Re: Remove Sequence Nbr From Filename

#5 Post by marvlus » 12 Mar 2012 14:59

aGerman,
thanks for your response too! It will pay to have extra protection if there are more spaces!

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

Re: Remove Sequence Nbr From Filename

#6 Post by aGerman » 12 Mar 2012 15:03

Yeah, it also checks whether the file name begins with a numeric expression + space. In case you would run it twice already renamed files are untouched.

Regards
aGerman

Post Reply