batch file code to find&replace filenames

January 20, 2010 at 07:42:51
Specs: Windows XP
Hello there. I want to be able to run a batch file to find filename xxyyzz and rename it to 12_xxyyzz. this:

which is the clsoet i've come, only appends a prefix to every file.

See More: batch file code to find&replace filenames

Report •

January 20, 2010 at 12:10:31
i only just realised you ment diffent file names, if you use the find command then use rename, REN *.fileextention to *.desiredextension

if you wish to rename them individually a differnt filename and not extension i am not sure, sorry but i didnt quite understand your post so iposted several possible sources.

use the find command with whatever perimeter you require, then in the line below use the ren command

REN "%random%.doc %random%.txt

Searches for a text string in a file or files.

FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string"
[[drive:][path]filename[ ...]]

/V Displays all lines NOT containing the specified
/C Displays only the count of lines containing the
/N Displays line numbers with the displayed lines.
/I Ignores the case of characters when searching for
the string.
/OFF[LINE] Do not skip files with offline attribute set.
"string" Specifies the text string to find.
Specifies a file or files to search.

If a path is not specified, FIND searches the text typed at the
or piped from another command.

Report •

January 20, 2010 at 13:38:29
or do you mean like:
if "%1" neq "" (ren %1 12_%1) else (echo usage: %0 filename ## renames filename to 12_filename ##)
(or alternatively:)
set /p xx=enter filename:
ren %xx% 12_%xx%

Report •

January 21, 2010 at 06:02:00
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

January 21, 2010 at 10:10:00
just the two, (123_xxxyyyzzz and 456_dddffffzzz)? or more? and if more, about how many?
if there is no way to distinguish the ones you want to rename from the ones you don't, you will have to make a table of names.

Report •

January 21, 2010 at 14:50:42
I had the same question ... if you change the code for a second one, then you may say there is a third one ... well, and one that works differently ;)

What's the saying, two's company, three is a crowd ? Anyway, please specify ALL the details at once !

Report •

January 22, 2010 at 04:11:42
this is exactly what I wanted:

@echo off & setLocal EnableDELAYedeXpansion

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

Report •

January 23, 2010 at 05:59:54
I have the same remark as in the other thread, but the above code is the same as doing this :

ren xxxyyyzzz 123_xxxyyyzzz
ren ccchhhyyy 456_ccchhhyyy

The /B parameter of DIR will only show the filename. So, no path. And if you are looking in a given directory, not looking into subdirectories ... well, you know what you will end up with.

The code could be necessary, if you use it slightly different, but here it's a case of overcoding.

Report •

Ask Question