Batch file to find&replace filename

January 20, 2010 at 07:36:35
Specs: Windows XP
I want to be able to run a batch file to find filename xxyyzz and rename it to 12_xxyyzz. this: http://www.computing.net/answers/pr...

is the clsoet i've come but it only appends a prefix to every file.


See More: Batch file to find&replace filename

Report •


#1
January 20, 2010 at 12:06:49
let me take an untested stab at it...

@echo off
for /f "tokens=*" %%a in ('dir xxyyzz /s /b') do ren "%%a" 12_xxyyzz


Report •

#2
January 20, 2010 at 23:00:39
"it only appends a prefix to every file. "

And your requirement is... ?


=====================================
Helping others achieve escape felicity

M2


Report •

#3
January 21, 2010 at 06:15:39
Hello there - i really appreciate your help. And you're right - i've explained this badly.
Where I have a bunch of files with various extensions sitting in a single location with the names:

xxxyyyzzz, dddffffzzz, ccchhhzzz

I want to be able to append the prefix '123_' where filename contains 'xxxyyy'
and '456_' where filename contains 'dddfff'
to give:
123_xxxyyyzzz and 456_dddffffzzz


Report •

Related Solutions

#4
January 21, 2010 at 07:58:04
@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir/b ^| find "xxxyyyzzz"') do (
ren %%a 123_%%a
)


=====================================
Helping others achieve escape felicity

M2


Report •

#5
January 22, 2010 at 05:06:27
Brilliant this works exactly how I wanted. Thank you.

Now i've realised that the directory will be zipped by a separate process and so would like to run the script from a location separate to the files being renamed. Is this possible?


Report •

#6
January 22, 2010 at 05:32:25
If your files are in c:\files

==============================

@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir/b/s/a-d c:\files ^| find "xxxyyyzzz"') do (
ren "%%a" "123_%%~Na%%~Xa"
)


=====================================
Helping others achieve escape felicity

M2


Report •

#7
January 22, 2010 at 07:45:51
> @echo off & setLocal EnableDELAYedeXpansion
>
> for /f "tokens=* delims= " %%a in ('dir/b ^| find "xxxyyyzzz"') > do (
> ren %%a 123_%%a
> )
>

(this is to Response nr. 4 - just a question by me, not directly related to the question in Response nr. 5)

Just a question M2, but if you use only /B with DIR, and not /S, he is not going to show the path anyway ...

So, he would only end up with ... "xxxyyyzzz" (if it exist). So, the code could be:

if exist xxxyyyzzz rename xxxyyyzzz 123_xxxyyyzzz

No ?


Report •


Ask Question