Sign in with
Sign up | Sign in
Your question

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

Last response: in Windows XP
Share
Anonymous
June 8, 2005 1:57:01 PM

Archived from groups: microsoft.public.windowsxp.general (More info?)

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
Anonymous
June 8, 2005 2:42:01 PM

Archived from groups: microsoft.public.windowsxp.general (More info?)

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.

--
Jonybrv

"NASAengr" wrote:

> 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
Anonymous
June 8, 2005 4:28:03 PM

Archived from groups: microsoft.public.windowsxp.general (More info?)

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.

--
Jonybrv


"Jonybrv" wrote:

> 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.
>
> --
> Jonybrv
>
> "NASAengr" wrote:
>
> > 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
Related resources
Can't find your answer ? Ask !
Anonymous
June 8, 2005 4:49:11 PM

Archived from groups: microsoft.public.windowsxp.general (More info?)

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 ? ;) 
--
Jonybrv


"Jonybrv" wrote:

> 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.
>
> --
> Jonybrv
>
>
> "Jonybrv" wrote:
>
> > 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.
> >
> > --
> > Jonybrv
> >
> > "NASAengr" wrote:
> >
> > > 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
Anonymous
June 8, 2005 6:47:07 PM

Archived from groups: microsoft.public.windowsxp.general (More info?)

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" wrote:

>
> 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 ? ;) 
> --
> Jonybrv
>
>
> "Jonybrv" wrote:
>
> > 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.
> >
> > --
> > Jonybrv
> >
> >
> > "Jonybrv" wrote:
> >
> > > 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.
> > >
> > > --
> > > Jonybrv
> > >
> > > "NASAengr" wrote:
> > >
> > > > 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
Anonymous
June 8, 2005 7:04:02 PM

Archived from groups: microsoft.public.windowsxp.general (More info?)

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" wrote:

> 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" wrote:
>
> >
> > 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 ? ;) 
> > --
> > Jonybrv
> >
> >
> > "Jonybrv" wrote:
> >
> > > 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.
> > >
> > > --
> > > Jonybrv
> > >
> > >
> > > "Jonybrv" wrote:
> > >
> > > > 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.
> > > >
> > > > --
> > > > Jonybrv
> > > >
> > > > "NASAengr" wrote:
> > > >
> > > > > 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
Anonymous
June 8, 2005 10:04:31 PM

Archived from groups: microsoft.public.windowsxp.general (More info?)

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.

"NASAengr" <NASAengr@discussions.microsoft.com> wrote in message
news:D 7A6DCD4-06F0-47B9-9F35-61C42C1D2F17@microsoft.com...
> 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
Anonymous
June 9, 2005 2:22:38 PM

Archived from groups: microsoft.public.windowsxp.general (More info?)

"NASAengr" <NASAengr@discussions.microsoft.com> wrote in message
news:D 7A6DCD4-06F0-47B9-9F35-61C42C1D2F17@microsoft.com...
> 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
February 7, 2008 2:55:26 AM

hi guys, this basically does the same as the "notepad approach"
enjoy :) 



@REM=========================================================
@REM renx.bat by /adhs all rights resigned
@REM rename multiple files with string search and replace
@REM=========================================================

@if "%1" NEQ "" @goto go
@echo USAGE:
@echo renx (/T /R) filemask searchstring replacestring
@echo /T print filenames only
@echo /R recurse subdirectories
@echo.

:go
@set rencmd=move
@set rec=

:parseopts
@if /i "%1" =="/T" set rencmd=rem && shift && goto parseopts
@if /i "%1" =="/R" set rec=/R && shift && goto parseopts

@for %rec% %%x in (%1) DO @call :RenameFile "%%x" %2 %3
@goto end

@REM=======================================================
@REM RenameFile (filename, searchstring, replacestring)
@REM=======================================================
:RenameFile
@set name=%1
@>tmp.bat echo @set newname=%%name:%2=%3%%
@>>tmp.bat echo @echo %%name%%
@>>tmp.bat echo @echo %%newname%%
@>>tmp.bat echo @echo.
@>>tmp.bat echo @%rencmd% %name% %%newname%%
@call tmp.bat
@del tmp.bat
@goto end
:end


hint:
quote only where necessary.
it's not only annoying and makes threads unreadable
it's also useless :p 

July 26, 2010 4:18:13 AM

adhs,

Your routine is good but it has a bug. It makes multiple passes against the same file.

Try the following;

filename 1 - abc-123.txt
filename 2 - abc-456.txt

renx abc* abc abcd

This will change the first file to abcdd-123.txt. The second file is ok. This can be corrected by using abc-* as the filename template.
!