Batch rename- string replace in file name

November 13, 2011 at 16:06:28
Specs: Windows XP
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:

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,


See More: Batch rename- string replace in file name

Report •

November 13, 2011 at 18:28:49
Hey buddy i know one great application for this purpose if you just want to rename files then ill upload it & you want code then i dont know

Report •

November 13, 2011 at 18:37:03
Please test this, replacing paths to suit your system.

@echo off
setlocal enabledelayedexpansion 

for /f "tokens=*" %%1 in ('dir /b /a-d %temp%\A3*.txt') do (
    set filname=%%1

        for /f "tokens=1* delims=-" %%A in ("!filname!") do (
            set newname=%%B
            ren %temp%\!filname! "a1 - !newname!"

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

Report •

Related Solutions

Ask Question