Using RENAME option in CMD

G

Guest

I am trying to rename a group of files in the Command window (CMD) (DOS,
Windows XP), which are of the
order of '001_01.jpg', by using the option "rename *.jpg cd1_*.*", where
"cd1_" is added to the front of every file.

Unfortunately, when I apply the rename option to "022_19.jpg", I get
"cd1_19.jpg", instead of "cd1_022_19.jpg". Is there any reason for the
missing characters? (I think it is something to with the "_" character.)

John F.
 
P

Pegasus \(MVP\)

John F. said:
I am trying to rename a group of files in the Command window (CMD) (DOS,
Windows XP), which are of the
order of '001_01.jpg', by using the option "rename *.jpg cd1_*.*", where
"cd1_" is added to the front of every file.

Unfortunately, when I apply the rename option to "022_19.jpg", I get
"cd1_19.jpg", instead of "cd1_022_19.jpg". Is there any reason for the
missing characters? (I think it is something to with the "_" character.)

John F.

The behaviour of the ren command can be somewhat unpredictable.
Try this instead:

for %a in (*.jpg) do ren "%a" "cd1_%a"
 
G

Guest

Thanks, this did solve my problem - all files now amended.

However, using it in Windows 2000, the last file had a prefix added twice,
thus giving "cd1_cd1_022_19a.jpg". But, with a large number of files (62),
the code kept adding "cd1_" indefinitely, until it ran out of space (ie. up
to 255 characters.)

In Windows XP, after 18 files, it again added "cd1" twice, but stopped when
it reached the last one (74 files).
 
P

Pegasus \(MVP\)

The repetition you observed can happen when the system fails
to realise that some files have already renamed. To avoid this,
you need to take a snapshot at the beginning, then perform the
rename. The following batch file will do it:

@echo off
dir *.jpg /b > files.lst
for /F "tokens=*" %%* in (files.lst) do ren "%%*" "cd1%%*"
del files.lst
 

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