Command Line Question about Renaming Files

  • Thread starter Mark & Mary Ann Weiss
  • Start date
M

Mark & Mary Ann Weiss

I have a large number of files that begin with "01-", 02-", and so on. The
filenames are varying lengths, but the extension is always 3 characters.

Using the Windows 2000 command line, is it possible to batch rename these
files such that the first three characters are stripped from the filename?
If so, can someone post an example?

--
Take care,

Mark & Mary Ann Weiss

VIDEO PRODUCTION . FILM SCANNING . AUDIO RESTORATION
Hear my Kurzweil Creations at: http://www.dv-clips.com/theater.htm
Business sites at:
www.dv-clips.com
www.mwcomms.com
www.adventuresinanimemusic.com
-
 
P

Pegasus \(MVP\)

Mark & Mary Ann Weiss said:
I have a large number of files that begin with "01-", 02-", and so on. The
filenames are varying lengths, but the extension is always 3 characters.

Using the Windows 2000 command line, is it possible to batch rename these
files such that the first three characters are stripped from the filename?
If so, can someone post an example?

--
Take care,

Mark & Mary Ann Weiss

VIDEO PRODUCTION . FILM SCANNING . AUDIO RESTORATION
Hear my Kurzweil Creations at: http://www.dv-clips.com/theater.htm
Business sites at:
www.dv-clips.com
www.mwcomms.com
www.adventuresinanimemusic.com
-

Try this batch file. Note that it won't to its job properly
if you end up with ambiguous names. For example, if
you have these two files

HisMusic.rm
HerMusic.rm

then there will be an obvious clash.

@echo off
if exist %temp%\NewName.bat del %temp%\NewName.bat

for %%* in (*.*) do call :Strip %%*
call %temp%\NewName.bat
del %temp%\NewName.bat
goto :eof

:Strip
set name=%*
set NewName=%name:~3%
echo ren "%Name%" "%NewName%" >> %temp%\newname.bat
 
P

Pegasus \(MVP\)

Pegasus (MVP) said:
Try this batch file. Note that it won't to its job properly
if you end up with ambiguous names. For example, if
you have these two files

HisMusic.rm
HerMusic.rm

then there will be an obvious clash.

@echo off
if exist %temp%\NewName.bat del %temp%\NewName.bat

for %%* in (*.*) do call :Strip %%*
call %temp%\NewName.bat
del %temp%\NewName.bat
goto :eof

:Strip
set name=%*
set NewName=%name:~3%
echo ren "%Name%" "%NewName%" >> %temp%\newname.bat

To avoid unpleasant surprises because of unexpected behaviour,
you should probably run this version of the batch file first. It only
pretends to go through the motions but it won't rename any files.

@echo off
if exist %temp%\NewName.bat del %temp%\NewName.bat

for %%* in (*.*) do call :Strip %%*
echo Examine %temp%\NewName.bat to see what will happen,
echo then execute %temp%\NewName.bat.

rem call %temp%\NewName.bat
rem del %temp%\NewName.bat
goto :eof

:Strip
set name=%*
set NewName=%name:~3%
echo ren "%Name%" "%NewName%" >> %temp%\newname.bat
 
M

Mark & Mary Ann Weiss

Try this batch file. Note that it won't to its job properly
To avoid unpleasant surprises because of unexpected behaviour,
you should probably run this version of the batch file first. It only
pretends to go through the motions but it won't rename any files.

@echo off
if exist %temp%\NewName.bat del %temp%\NewName.bat

for %%* in (*.*) do call :Strip %%*
echo Examine %temp%\NewName.bat to see what will happen,
echo then execute %temp%\NewName.bat.

rem call %temp%\NewName.bat
rem del %temp%\NewName.bat
goto :eof

:Strip
set name=%*
set NewName=%name:~3%
echo ren "%Name%" "%NewName%" >> %temp%\newname.bat

Looks like this didn't work:
RESULT:

@echo off
if exist %temp%\NewName.bat del %temp%\NewName.bat

for %%* in (*.*) do call :Strip %%*
%%* was unexpected at this time. <<<<<<<<<<<<<<<error?
call %temp%\NewName.bat
'C:\DOCUME~1\MARKWE~1\LOCALS~1\Temp\NewName.bat' is not recognized as an
interna
l or external command,
operable program or batch file.
del %temp%\NewName.bat
Could Not Find C:\DOCUME~1\MARKWE~1\LOCALS~1\Temp\NewName.bat
goto :eof

:Strip
set name=%*
set NewName=%name:~3%
echo ren "%Name%" "%NewName%" >> %temp%\newname.bat


I have a vague idea of what is intended and that the key here is the "~3%"
portion, but I tried the set command one line at a time to see if the
environment would accept it and all that became persistent was "set
name=%*". The line after it might be invalid, as setting that does not
include it in the environment.

What exactly does the "~3%" statement do? Does it mean "delete first three
characters? What does "~" mean in this context?


--
Take care,

Mark & Mary Ann Weiss

VIDEO PRODUCTION . FILM SCANNING . AUDIO RESTORATION
Hear my Kurzweil Creations at: http://www.dv-clips.com/theater.htm
Business sites at:
www.dv-clips.com
www.mwcomms.com
www.adventuresinanimemusic.com
-
 
P

Pegasus \(MVP\)

Mark & Mary Ann Weiss said:
Looks like this didn't work:
RESULT:

@echo off
if exist %temp%\NewName.bat del %temp%\NewName.bat

for %%* in (*.*) do call :Strip %%*
%%* was unexpected at this time. <<<<<<<<<<<<<<<error?
call %temp%\NewName.bat
'C:\DOCUME~1\MARKWE~1\LOCALS~1\Temp\NewName.bat' is not recognized as an
interna
l or external command,
operable program or batch file.
del %temp%\NewName.bat
Could Not Find C:\DOCUME~1\MARKWE~1\LOCALS~1\Temp\NewName.bat
goto :eof

:Strip
set name=%*
set NewName=%name:~3%
echo ren "%Name%" "%NewName%" >> %temp%\newname.bat


I have a vague idea of what is intended and that the key here is the "~3%"
portion, but I tried the set command one line at a time to see if the
environment would accept it and all that became persistent was "set
name=%*". The line after it might be invalid, as setting that does not
include it in the environment.

What exactly does the "~3%" statement do? Does it mean "delete first three
characters? What does "~" mean in this context?


--
Take care,

Mark & Mary Ann Weiss

VIDEO PRODUCTION . FILM SCANNING . AUDIO RESTORATION
Hear my Kurzweil Creations at: http://www.dv-clips.com/theater.htm
Business sites at:
www.dv-clips.com
www.mwcomms.com
www.adventuresinanimemusic.com
-

I am puzzled as to what's going on on your machine. Are
you running Win2000?

I have modified the batch file to add some diagnostics.
Run it again until it throws up the first error, then post
a screen dump.

@echo on
if exist "%temp%\NewName.bat" del "%temp%\NewName.bat"

for %%* in (*.*) do (call :Strip %%* & pause)
echo Examine "%temp%\NewName.bat" to see what will happen,
echo then execute "%temp%\NewName.bat."

rem call "%temp%\NewName.bat"
rem del "%temp%\NewName.bat"
goto :eof

:Strip
set name=%*
set NewName=%name:~3%
echo ren "%Name%" "%NewName%" >> "%temp%\newname.bat"
 
M

Mark & Mary Ann Weiss

:Strip
I am puzzled as to what's going on on your machine. Are
you running Win2000?

I have modified the batch file to add some diagnostics.
Run it again until it throws up the first error, then post
a screen dump.

@echo on
if exist "%temp%\NewName.bat" del "%temp%\NewName.bat"

for %%* in (*.*) do (call :Strip %%* & pause)
echo Examine "%temp%\NewName.bat" to see what will happen,
echo then execute "%temp%\NewName.bat."

rem call "%temp%\NewName.bat"
rem del "%temp%\NewName.bat"
goto :eof

:Strip
set name=%*
set NewName=%name:~3%
echo ren "%Name%" "%NewName%" >> "%temp%\newname.bat"

Win2K SP4 here, to be exact.
I found the problem...
It had to do with how I was sending the commands to the command processor.
I assumed that I could paste it into the cmd window like I do with single
line commands, but apparently the bat file has to exist on the drive for
this bat file to work.
So I pasted to a text file, renamed it 'ren.bat' and copied it to the folder
where I wanted to strip out the first three numerical characters. It works.
It does rename itself in the process and generates an error, but all the
files I wanted renamed are renamed appropriately.

I don't fully understand how your batch file works, but thanks for providing
a solution to my batch renaming challenge.


--
Take care,

Mark & Mary Ann Weiss

VIDEO PRODUCTION . FILM SCANNING . AUDIO RESTORATION
Hear my Kurzweil Creations at: http://www.dv-clips.com/theater.htm
Business sites at:
www.dv-clips.com
www.mwcomms.com
www.adventuresinanimemusic.com
-
 
P

Pegasus \(MVP\)

Mark & Mary Ann Weiss said:
Win2K SP4 here, to be exact.
I found the problem...
It had to do with how I was sending the commands to the command processor.
I assumed that I could paste it into the cmd window like I do with single
line commands, but apparently the bat file has to exist on the drive for
this bat file to work.
So I pasted to a text file, renamed it 'ren.bat' and copied it to the folder
where I wanted to strip out the first three numerical characters. It works.
It does rename itself in the process and generates an error, but all the
files I wanted renamed are renamed appropriately.

I don't fully understand how your batch file works, but thanks for providing
a solution to my batch renaming challenge.


--
Take care,

Mark & Mary Ann Weiss

VIDEO PRODUCTION . FILM SCANNING . AUDIO RESTORATION
Hear my Kurzweil Creations at: http://www.dv-clips.com/theater.htm
Business sites at:
www.dv-clips.com
www.mwcomms.com
www.adventuresinanimemusic.com
-

The batch file I wrote must obviously NOT reside in the
folder where the files are to be renamed. This would be
equivalent to sawing off the branch on which you sit . . .
Place it into c:\winnt!
 

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