"rename" with wildcards in cmd.exe under XP doesn't work correctly

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In the DOS command window, using a command like

rename Picture*.jpg April2005*.jpg

overwrites the first nine characters (the length of "April2005", not seven,
the length of "Picture") with "April2005". The result might be

Picture-21
Picture-22

becomes

April20051
April20052

What is the best workaround to achieve names

April2005-01
April2005-02
 
I tried to simulate this by using wild card ? and it did not work.

However, found a workaround.

dir /b picture*.txt > list.bat
notepad list.bat
Edit menu | replace

Find what: picture-2
Replace with:Ren April2005-0

Save the file.

At command prompt run list.bat to complete.
 
fwiw,
Maybe you can, from within a windows folder, select all the images then
rename april2005-.jpg whilst holding F2 to finish ... then windows will
apply sequence numbers to all highlighted files.
 
I am really sorry that's a wrong command.

Try this line alone in a batch file. ex:list.bat

for /F "tokens=1-4 delims=-" %%1 in ( 'dir /b picture*' ) do ren %%1-%%2
April2005-%%2

That's a single line. It look for picture as %%1 and 2x.txt as %%2 and then
replaces %%1 with April2005. It will not rename 2 ;(

Hope this helps. I would not think of a simpler method.
 
More perfect:

for /F "tokens=1-4 delims=-2" %%1 in ( 'dir /b picture*' ) do ren %%1-2%%2
April2005-0%%2

It should fail for only one file. Guess which ? ;)
 
The batch file and loop ideas are working, but I'm still having the same
problem with the rename command on the individual files without the
wildcards. I adjusted your code to pass in the parameters by creating a
batch file "r2" consisting of

Rem call "r2 fromprefix toprefix" to rename files beginning with fromprefix
to files beginning with toprefix
for /F "tokens=1-4 delims=-2" %%A in ( 'dir /b %1*' ) do ren %%A%%B %2%%B

I'm not sure what purpose %%B (you used "%%2") serves. Also, my XP Help and
Support Center says variables must be alpha (I quote):

Syntax
for {%variable|%%variable} in (set) do command [ CommandLineOptions]

Parameters
{%variable|%%variable}
Required. Represents a replaceable parameter. Use %variable to carry out for
from the command prompt. Use %%variable to carry out the for command within a
batch file. Variables are case-sensitive and must be represented with an
alpha value, such as %A, %B, or %C.
 
Alpha or numbers that does not matter. See. let me explain.

"/F tokens=1-4" is a range. This means I will be using four variables after
splitting the result depending on the delimiter which is "-2". For the file
"picture-21.txt" should split itself into "picture" and "1.txt" ( two
variables). I can use any alpha or number with %% to get the values.

In my code, %%2 indicates "1.txt". As you needed 0 before it I changed it to
0%%2.

Burntracks seems to be suggesting something easy. Try that too.
--
Jonybrv


NASAengr said:
The batch file and loop ideas are working, but I'm still having the same
problem with the rename command on the individual files without the
wildcards. I adjusted your code to pass in the parameters by creating a
batch file "r2" consisting of

Rem call "r2 fromprefix toprefix" to rename files beginning with fromprefix
to files beginning with toprefix
for /F "tokens=1-4 delims=-2" %%A in ( 'dir /b %1*' ) do ren %%A%%B %2%%B

I'm not sure what purpose %%B (you used "%%2") serves. Also, my XP Help and
Support Center says variables must be alpha (I quote):

Syntax
for {%variable|%%variable} in (set) do command [ CommandLineOptions]

Parameters
{%variable|%%variable}
Required. Represents a replaceable parameter. Use %variable to carry out for
from the command prompt. Use %%variable to carry out the for command within a
batch file. Variables are case-sensitive and must be represented with an
alpha value, such as %A, %B, or %C.


Jonybrv said:
More perfect:

for /F "tokens=1-4 delims=-2" %%1 in ( 'dir /b picture*' ) do ren %%1-2%%2
April2005-0%%2

It should fail for only one file. Guess which ? ;)
 
NASAengr said:
In the DOS command window, using a command like

rename Picture*.jpg April2005*.jpg

overwrites the first nine characters (the length of "April2005", not
seven,
the length of "Picture") with "April2005". The result might be

Picture-21
Picture-22

becomes

April20051
April20052

What is the best workaround to achieve names

April2005-01
April2005-02

if you're not fussed about leading zeroes,

for /l %i in (1,1,99) do ren picture*.jpg april2005-%i.jpg 2>nul

(double "%" to use within a batch)

the "2>nul" part suppresses error messages that would otherwise occur for
the attempted rename of picture* to a filename that already exists.

If you ARE fussed, then
for /l %i in (1,1,9) do ren picture*.jpg april2005-0%i.jpg 2>nul
for /l %i in (10,1,99) do ren picture*.jpg april2005-%i.jpg 2>nul

....and follow the bouncing ball if you want 3 digits

NT/2K/XP batch discussion group : alt.msdos.batch.nt
pre-NT/2K/XP batch discussion group : alt.msdos.batch

HTH

....Bill
 
Back
Top