Archived from groups: microsoft.public.windowsxp.basics (
More info?)
"JRSNHECI" <JRSNHECI@discussions.microsoft.com> wrote in message news:ACA60D76-E791-4A1E-A016-834F59FAB434@microsoft.com...
>I have a series of 200 files with file names in the format 00000### where ###
> is a 3-digit number. I'd like to find an easy way to truncate the leading
> zeros so that the file name is just ###. I need to preserve the actual
> number as opposed to issuing a new series of sequential numbers. Any ideas?
> Thanks!
In a batch file called, say "trimmer.bat", add the following lines inside it, and then execute it entering "cmd /v /c <path>trimmer" in a DOS shell after you have navigated to the directory with your files (the /v switch is required to force immediate expansion of environment variables within the for-loop). The <path> isn't needed if you save trimmer.bat in a directory listed in your PATH environment variable. I added D:\Batch to my PATH and that's where I save by .bat files.
File: trimmer.bat
--------------------
@echo off
rem --- MAIN
for %%a in (*) do (
set origname=%%a
set modname=!origname!
call :LeftTrim 0
echo Old name = "!origname!"
echo New name = "!modname!"
ren "!origname!" "!modname!"
echo.
)
goto :EOF
rem --- FUNCTIONS
:LeftTrim
rem - Deletes specified leading prefix character(s), plus any leading space(s)
rem - after the delete. If prefix character omitted, delete leading spaces.
rem - Syntax: call :LeftTrim [char]
rem (uses global variable "modname")
if "%1" == "" goto DelSpace
rem - Use only the first character of the input parameter.
set delchar=%1
set delchar=!delchar:~0,1!
rem - Remove the prefix character(s).
elPrefix
if "!modname:~0,1!" == "!delchar!" (
set modname=!modname:~1!
goto DelPrefix
)
rem - Remove any [resultant] leading spaces.
elSpace
if "!modname:~0,1!" == " " (
set modname=!modname:~1!
goto DelSpace
)
set delchar=
goto :EOF