File Rename Utility

D

Dragon

Does anyone know of a utility that allows you to get renaming criteria from
a file and then rename the files in a particular folder. for Example:

Files Names (Current)

P001A_asdsd.xyz
Q001B_fdsdffdd.xyz
R001_dsdsdsds.xyz
P002A_asasa.xyz
P002B_hghghgh.xyz
Q003_rerere.xyz

Criteria in the File:
Look for: *002* Rename to BBBBBBBB_%fileName%
Look for: *003* Rename to CCCCCCCC_%fileName%

Result

P001A_asdsd.xyz
Q001B_fdsdffdd.xyz
R001_dsdsdsds.xyz
BBBBBBBB_P002A_asasa.xyz
BBBBBBBB_P002B_hghghgh.xyz
CCCCCCCC_Q003_rerere.xyz

Thank you.
 
P

Pegasus \(MVP\)

Dragon said:
Does anyone know of a utility that allows you to get renaming criteria from
a file and then rename the files in a particular folder. for Example:

Files Names (Current)

P001A_asdsd.xyz
Q001B_fdsdffdd.xyz
R001_dsdsdsds.xyz
P002A_asasa.xyz
P002B_hghghgh.xyz
Q003_rerere.xyz

Criteria in the File:
Look for: *002* Rename to BBBBBBBB_%fileName%
Look for: *003* Rename to CCCCCCCC_%fileName%

Result

P001A_asdsd.xyz
Q001B_fdsdffdd.xyz
R001_dsdsdsds.xyz
BBBBBBBB_P002A_asasa.xyz
BBBBBBBB_P002B_hghghgh.xyz
CCCCCCCC_Q003_rerere.xyz

Thank you.

Try this:

@echo off
for /F "tokens=*" %%* in ('dir /b *002*.*') do echo ren "%%*" "BBBBBBBB%%*"
for /F "tokens=*" %%* in ('dir /b *003*.*') do echo ren "%%*" "CCCCCCCC%%*"

Remove "echo" when you're satisfied that it does what you want it to do.
 
D

Dragon

Thank you. It seems to work great except that I found a couple of anomalies.

My commands were:

for /F "tokens=*" %%* in ('dir /b *051*.*') do ren "%%*" "%%* ABC.XYZ
for /F "tokens=*" %%* in ('dir /b *052*.*') do ren "%%*" "%%* DEF.XYZ
for /F "tokens=*" %%* in ('dir /b *053*.*') do ren "%%*" "%%* GHI.XYZ
.... Up to *114*...

For some reason it picked up file names with 001,011,014 etc that were not
part of the criteria. Any ideas?

Thank you.
 
R

Rob Stow

Dragon said:
Thank you. It seems to work great except that I found a couple of anomalies.

My commands were:

for /F "tokens=*" %%* in ('dir /b *051*.*') do ren "%%*" "%%* ABC.XYZ
for /F "tokens=*" %%* in ('dir /b *052*.*') do ren "%%*" "%%* DEF.XYZ
for /F "tokens=*" %%* in ('dir /b *053*.*') do ren "%%*" "%%* GHI.XYZ
... Up to *114*...

For some reason it picked up file names with 001,011,014 etc that were not
part of the criteria. Any ideas?

I know exactly why that is happening and it is
not your fault - MicroSoft f*cked up in a big way.

Something like "dir *token*.*" not only picks up
all files containing "token" in the filename, but
also all files containing "token" in the 8.3 filename.

Take a look at the results of "dir /x", which will
display both the long and 8.3 names of the files.

Normally the 8.3 filenames don't cause a problem
because they are usually just a truncated version
of the long file name, but sometimes there are
incongruities between the 8.3 and long names.

For that reason, you should *never* trust the output
of expressions that use wildcards (* or ?). You can
use the wildcarded expression to generate a list of
files or folders to process, but you still need to
inspect each file/folder on that list before doing
anything with it.

I suggest you try modifying your batch file so that
it checks for a tilde in the filename. If no tilde,
then procede with the rename, else do nothing.
 
D

Dragon

I totally forgot about the 8.3. I guess I will have to modify my batch file.
:)

Thanks.
 
P

Pegasus \(MVP\)

By putting his finger right on it, Rob saved me a lot of trouble-shooting
time. Here is an alternative that avoids this trap. Unfortunately it runs
much more slowly than the first version.

@echo off
dir /b /a-d > \dir.tmp
for /F "tokens=*" %%* in ('type \dir.tmp ^| find /i "051"') do echo ren
"%%*" "%%* % ABC.xyz"
for /F "tokens=*" %%* in ('type \dir.tmp ^| find /i "052"') do echo ren
"%%*" "%%* % DEF.xyz"
del \dir.tmp
 
R

Rob Stow

Dragon said:
I totally forgot about the 8.3. I guess I will have to modify my batch file.
:)

I had my own blonde moment. Modifying the batch
file the way I suggested is not an option because
the output of "dir /b" doesn't give you any 8.3 file
names to work with. Instead of checking for the
absence of a tilde, you would need to check for the
presence of the target string, "051" for example.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top