Re: Renaming multiple files



PcCare used advanced batch file techniques in his reply.
Here is a brief explanation of each line:

for /r %%i in (*.jpg) do call :renameit %%~nxi

This command enumerates all .jpg files in the current
folder and invokes the "rename" subroutine, passing
as a parameter just the file name (the "~n" component)
plus the extension (the "~i" component) but not the
path name. You see find a full explanation when you
type for /? at the Command Prompt.

goto :EOF

This command causes the batch file to terminate after
it has dealt with all .jpg files.

:renameit

This is the starting label for the subroutine.

set filename=%1

This command sets the variable "filename" to the value
that gets passed from the main program. It should actually
read as follows to deal with file names that have embedded
spaces:

set filename=%*

echo Renaming %filename% to %filename:0=%

This line gives you on-screen feedback.

rename %filename% %filename:0=%

This line performs the renaming. It translates every
"0" character to a blank. The command is explained
when you type set /? at the Command Prompt.
In its current form it actually has a flaw: It will remove
ALL "0"s, regardless of where they occure. Thus it
will do both of the following:

000137.jpg -> 137.jpg
000130.jpg -> 13.jpg

goto :EOF

This command is superfluous. It can be omitted.


for /r %%i in (*.jpg) do call :renameit %%~nxi

goto :EOF

:renameit
set filename=%1
echo Renaming %filename% to %filename:0=%
rename %filename% %filename:0=%
goto :EOF
"PSRumbagh" <PSRumbagh@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:85147906-3842-40F6-AD21-AB8A6414DF0A@xxxxxxxxxxxxxxxx
Thanks. The batch file works great. Could you explain to a neophyte what
each line does? Can you refer me to a good tutorial on writting batch
files?

"PcCare" wrote:



copy this code into a command file, make sure you have a backup before
running

for /r %%i in (*.jpg) do call :renameit %%~nxi

goto :EOF

:renameit
set filename=%1
echo Renaming %filename% to %filename:0=%
rename %filename% %filename:0=%
goto :EOF



"PSRumbagh" wrote:

I have a file folder that contains hundreds of .jpg files named such as
im00002.jpg, im00018.jpg, im044.jpg, im00137.jpg, etc. Using some sort
of
batch process, I would like to rename them without all the leading
zeros,
i.e. im2.jpg, im18.jpg, im44.jpg, im137.jpg. How do I do this?


.



Relevant Pages

  • Re: Command Line Question about Renaming Files
    ... It had to do with how I was sending the commands to the command processor. ... this bat file to work. ... It does rename itself in the process and generates an error, ... I don't fully understand how your batch file works, ...
    (microsoft.public.win2000.general)
  • Re: Renaming files using wildcards
    ... When I strip down the filenames of their ... spaces, the rename works well. ... strip the filename of it's spaces, ... rename command can't find the file name to rename when it ...
    (microsoft.public.windowsxp.general)
  • Re: Cant Rename a File Using WinXP Command Prompt!!!
    ... It needs to be given a shorter name so that it can be ... I cannot rename it using the Command Prompt and the ... The old filename in question contains the following symbols as ...
    (microsoft.public.windowsxp.help_and_support)
  • Cant Rename a File Using WinXP Command Prompt!!!
    ... It needs to be given a shorter name so that it can be ... I've used the DIR command to list the files in this sub-directory. ... The old filename in question contains the following symbols as part ... The type of file I'm trying to rename is an askSam file>> *.ask ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: Cant Rename a File Using WinXP Command Prompt!!!
    ... It needs to be given a shorter name so that it can be ... I've used the DIR command to list the files in this sub-directory. ... The old filename in question contains the following symbols as part ... The type of file I'm trying to rename is an askSam file>> *.ask ...
    (microsoft.public.windowsxp.help_and_support)

Loading