Rename part of filenames via Batch

May 25, 2011 at 12:45:41
Specs: Windows XP
I would like to rename the first part of each file while keeping the rest intact, using a DOS BATCH file if possible.

On the site, there are others posting something similar, but the problem I am running into is because of the [space] in the filename, and that the filename isn't always the same number of chars.

Rename "C:\test\File Name XYZ_123456.txt" to "C:\test\NewFileName_123456.txt"
Rename "C:\test\File Name XYZ_123456789.txt" to "C:\test\NewFileName_123456789.txt"
Rename "C:\test\File Name XYZ_001122334455.txt" to "C:\test\NewFileName_001122334455.txt"
Rename "C:\test\File Name XYZ_123456abc.txt" to "C:\test\NewFileName_123456abc.txt"

As you can see, I just need to change the part "File Name XYZ" to "NewFileName"

the DOS command for rename (REN) seems to dislike the spacing.
When I try: REN File?Name?XYZ_*.txt NewFileName_*.txt

I get:
Original File of "File Name XYZ_123456.txt" renamed to "NewFileName_Z_123456.txt"

This leads me to think that REN uses postioning.

(note the space - that's the problem I seem to have)

Any Help would be greatly appreciated!

See More: Rename part of filenames via Batch

Report •

May 25, 2011 at 19:41:57
Is NewFileName literally part of the renamed filenames or does each renamed file have a new file name. If the new file names are unique where do they come from?

Please come back & tell us if your problem is resolved.

Report •

May 26, 2011 at 13:27:58
I simply used that as an example. the old file name is "Customer Name Plan_###.txt" and I need them all to be corrected to NewCustomerName_####.txt"

Report •

May 26, 2011 at 19:57:44
For me this is nowhere near specific enough.

You want Rename "C:\test\File Name XYZ_123456.txt" to "C:\test\NewFileName_123456.txt" , should this not be Rename "C:\test\File Name XYZ_123456.txt" to "C:\test\FileName_123456.txt" by simply removing the space and XYZ?

You show that the old filename is Customer Name Plan_###.txt but then you want it to be corrected to NewCustomerName_####.txt", where does the additional # come from? Can the Customer name contain more than one space e.g. John Cecil Magruder or is one space the absolute limit?

I may sound picky but these detail can make a lot of difference in how the script is written.

Please come back & tell us if your problem is resolved.

Report •

Related Solutions

May 27, 2011 at 11:03:28
Thank you for your help, I was able to resolve this with a FOR statement:

@Echo Off & SetLocal EnableExtensions
For /F "tokens=1* delims=_" %%I IN ('dir /a-d /b *.txt') DO rename "%%~I_%%~J" "NewfileName_%%~J"

Report •

Ask Question