Batch rename- string replace in file name

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jacDaRippa
Posts: 1
Joined: 13 Nov 2011 17:44

Batch rename- string replace in file name

#1 Post by jacDaRippa » 13 Nov 2011 17:55

Hello everyone,

I want to rename files but in doing so, i want to be able to replace matching text with another.
So only the text specified gets changed. I wish to do it in batch, more than one file at a time.

Currently i have the following (hope i outline it clear enough)

The file names i wish to to change are:
A3-12345_iuiu.TXT
A3-12365_opop.TXT

The desired result i want is
A1 - 12345_iuiu.TXT
A1 - 12365_opop.TXT

The code i have so far, it does kind of what i want, it replaces the A3 with A1 and puts the spaces but i want to replace the "A3-" with "A1 - "
REN D:\Program Files\A3-*.TXT "A1 - *.TXT"

The result i get is:
A1 - 345_iuiu.TXT
A1 - 365_opop.TXT

It removes the first 2 charactoers of the numbers, this is because it does a straight rename (replacing the 3 charcaters i specifed with the 5 new characters), is there are way to replace 3 characters with 5 and not lose any of the text?

Thanks in advance,

jac

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Batch rename- string replace in file name

#2 Post by dbenham » 13 Nov 2011 19:21

You will have to parse and build the names yourself.

Try this (double %% assumes batch file)

Code: Select all

for /f "tokens=1,* delims=-" %%A in ('dir /b "D:\Program Files\A3-*.TXT"') do ren "D:\Program Files\A3-%%B" "A1 - %%B"

The code may have to change significantly if the name and/or path patterns change.


Dave Benham

Post Reply