Tech Support > Operating Systems > MS-DOS > Batch File to remove leading zeros
Batch File to remove leading zeros
Posted by Leo on September 17th, 2004


I have to rename thousands of files that look like this: 00005879.txt

What I want to do is rename them all by removing the leading zeros so
that they look like: 5879.txt.

Any ideas or suggestions.

I thank you in advance.


Leo Ruiz

Posted by Matthias Tacke on September 17th, 2004


Leo wrote:
What exact OS?
Pure batch or are external programs possible?
Are ther always 4 zero's?
Is the file name always 8 places?

Some details help to find a solution.

--
Greetings
Matthias

Posted by Todd Vargo on September 18th, 2004



"Leo" <leonidesruiz@hotmail.com> wrote in message
news:cbde66ac.0409171246.458a167e@posting.google.c om...
Assuming some version of Windows 95 or newer, the following untested batch
should work for 8 character names.

::Untested.bat
@echo off
for %%? in (0000000?.txt) do ren %%? " *.*"
for %%? in (000000??.txt) do ren %%? " *.*"
for %%? in (00000???.txt) do ren %%? " *.*"
for %%? in (0000????.txt) do ren %%? " *.*"
for %%? in (000?????.txt) do ren %%? " *.*"
for %%? in (00??????.txt) do ren %%? " *.*"
for %%? in (0???????.txt) do ren %%? " *.*"
for %%? in (" *.txt") do ren "%%?" %%?
:: End of batch

--
Todd Vargo (double "L" to reply by email)



Posted by Michael Bednarek on September 18th, 2004


On 17 Sep 2004 13:46:30 -0700, leonidesruiz@hotmail.com (Leo) wrote in alt.msdos.batch:

Here's how I do that using 4DOS:

FOR %fn IN (0*.txt) REN %fn %@EVAL[%@NAME[%fn]].%@EXT[%fn]

This will fail if the filename contains non-digits. For that case,
the command could be (assuming the filename will always be 8
characters long):

FOR %fn IN ([0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].txt) REN %fn %@EVAL[%@NAME[%fn]].%@EXT[%fn]

If the length of the filename is variable, this command
might be used:

FOR %fn IN (0*.txt) IF %@NUMERIC[%@NAME[%fn]]=1 REN %fn %@EVAL[%@NAME[%fn]].%@EXT[%fn]

4DOS is freely available at <http://jpsoft.com\download.htm>;
for documentation see <http://jpsoft.com/help/>.

Other CLIs might require a more elaborate approach.

--
Michael Bednarek http://mbednarek.com/ "POST NO BILLS"

Posted by Klaus Meinhard on September 18th, 2004


Michael Bednarek wrote:

.... or simply use the %@replace function to replace leading zeroes with
nothing....

--
Mit freundlichem Gruß,

Klaus Meinhard



Posted by Dr John Stockton on September 18th, 2004


JRS: In article <cifjqi$fma$07$1@news.t-online.com>, dated Fri, 17 Sep
2004 23:09:39, seen in news:alt.msdos.batch, Matthias Tacke
<Matthias@Tacke.de> posted :
It is probably a bad idea, at least of the number of leading zeroes
varies. Wherever possible, numeric fields should be right-aligned.

Since this is news:alt.msdos.batch, you should presume that the OS is
MSDOS..Win98/ME.

You did not ask whether they are all in the same directory.

In versions of Windows with QBASIC, the task should be easy enough; but
I'm not familiar with it, having better tools.


A general technique is to list the files with dir /b , to edit the
list with a suitable tool to convert it to a list of command lines, and
then to inspect and execute the list.

To remove a fixed number of leading zeroes, say 3 (4 = 3+1),

dir 000*.* /b | COLS 'ren * 1- * 4- > $$$.BAT

To remove a variable number, either repeatedly remove one, or remove
8,7,6,5,4,3,2,1. But consider the case of a file numbered zero, which
may need special preliminary and terminal treatment (e.g. rename to
xxx.txt first; last to 0.txt).

For a more complicated rename, such as to remove all leading zeroes in a
single pass, use a general stream editor such as MiniTrue <URL:http://ww
w.idiotsdelight.net/minitrue/> or SED instead of COLS.

Get COLS via sig below.

Another possibility is to do dir /b > $$$1.dat then to process
$$$1.dat with QBASIC to give $$$2.BAT, then to execute $$$2.BAT.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk DOS 3.3, 6.20; Win98. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.

Posted by Richard Bonner on September 18th, 2004


*** If all the files have the same number of characters (8.3) and all
have four leading zeroes, and you want to get XSET, then try this
(untested) batch file:

:: ZERO.bat
::
@ECHO OFF

IF "%1" == *TASKS* GOTO TASKS

FOR %F IN (*.TXT) DO CALL ZERO.BAT *TASKS* %F
GOTO END

:TASKS
XSET /RIGHT 8 FILE="%2"
REN %2 %FILE%

:END


An XSET link is in my "DOS Websites" directory at:

http://www.chebucto.ns.ca/~ak621/DOS/Websites.html


Richard Bonner
http://www.chebucto.ns.ca/~ak621/DOS/


Posted by Charles Dye on September 18th, 2004


On Sat, 18 Sep 2004 09:07:47 +0200, "Klaus Meinhard"
<K_Meinhard@t-online.de> wrote:

@REPLACE will replace more than just the *leading* zeros! @LTRIM
might be a better choice.

--
Charles Dye raster@highfiber.com


Posted by Dr John Stockton on September 18th, 2004


JRS: In article <2r1k25F142it2U1@uni-berlin.de>, dated Fri, 17 Sep 2004
21:30:16, seen in news:alt.msdos.batch, Todd Vargo
<toddvargo@alvantage.com> posted :
You seem to be using unreasonable definitions of "remove" and/or "work".

Only tested solutions are trustworthy.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk DOS 3.3, 6.20; Win98. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.

Posted by Michael Bednarek on September 18th, 2004


On Sat, 18 Sep 2004 07:55:56 -0600, Charles Dye <raster@highfiber.com>
wrote in alt.msdos.batch:

That's why I didn't use @REPLACE. I didn't use @LTRIM because I don't
think it's part of 4DOS (it is part of 4NT/TC32 V-6, and probably
exactly the right tool). For 4DOS, my 3rd suggestion should work best.

--
Michael Bednarek http://mbednarek.com/ "POST NO BILLS"

Posted by Charles Dye on September 18th, 2004


On Sun, 19 Sep 2004 01:25:00 +1000, Michael Bednarek
<ROT13(zo@zorqanerx.pbz)> wrote:

Hmmm. You're right, it's not. I stand corrected.

--
Charles Dye raster@highfiber.com


Posted by Todd Vargo on September 19th, 2004



"Dr John Stockton" <spam@merlyn.demon.co.uk> wrote in message
news:+cmz7TF+aETBFwhe@merlyn.demon.co.uk...
Feel free to test the code to your hearts content.

Prove yourself trustworthy by doing your own testing before commenting.

--
Todd Vargo (double "L" to reply by email)



Posted by Dr John Stockton on September 20th, 2004


JRS: In article <2r66o3F1713kfU1@uni-berlin.de>, dated Sun, 19 Sep 2004
15:54:03, seen in news:alt.msdos.batch, Todd Vargo
<toddvargo@alvantage.com> posted :
I did. Your method failed. Of course, if you cannot tell the
difference between "remove" and "replace with space, thereby generating
file names of improper form", your method will have seemed to you to
pass.

But the OP sagaciously did provide an illustration : "00005879.txt"
should become "5879.txt" and not " 5879.txt" .

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk DOS 3.3, 6.20; Win98. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.

Posted by Leo on September 20th, 2004


Charles Dye <raster@highfiber.com> wrote in message news:<q8pok09uo6iqhbr3dkthi94jten823e3tt@4ax.com>. ..
I apologize for not specifying in the original post the important
detail of OS. It is in Windows2000, but I am assuming the way to do
this is through a batch file of some sort written for MS-DOS. My
problem comes in that I can't install 3rd party software to do this.
So, in essence I'm restricted to trying to find a DOS solution.

Posted by Matthias Tacke on September 20th, 2004


Leo wrote:
Depending on the OS solutions may differ heavily.
The proper group for nt/w2k/xp questions is news:alt.msdos.batch.nt

@echo off
set file=00005879.txt
:loop
if "%file:~0,1%" EQU "0" set file=%file:~1%&goto :loop
echo File:%file%

HTH
--
Greetings
Matthias

Posted by Matthias Tacke on September 20th, 2004


"Matthias Tacke" wrote:


::StripZero.cmd::::::::::::::::::::::::::::::::::: ::::::::::::::::::::
@echo off
set basedir=c:\test\2
pushd %basedir%
for /f "delims=" %%A in ('dir /S/B/A 0*.txt') do call :Strip0 "%%~fA"
popd
goto :eof
:Strip0
set file=%~xn1
:loop
if "%file:~0,1%" EQU "0" set file=%file:~1%&goto :loop
if not exist "%~dp1%file%" ren %1 %file%&goto :eof
echo Couldn't rename : %~1
echo file already exists: %~dp1%file%
::StripZero.cmd::::::::::::::::::::::::::::::::::: ::::::::::::::::::::

HTH

--
Greetings
Matthias

Posted by Todd Vargo on September 20th, 2004



"Dr John Stockton" <spam@merlyn.demon.co.uk> wrote
I see, my LFNFOR ON command is missing before the last FOR command. The
following is what was intended to be posted originally. This time it is
tested, and I will send you my bill in the morning. ;-)

:: Tested.bat
@echo off
for %%? in (0000000?.txt) do ren %%? " *.*"
for %%? in (000000??.txt) do ren %%? " *.*"
for %%? in (00000???.txt) do ren %%? " *.*"
for %%? in (0000????.txt) do ren %%? " *.*"
for %%? in (000?????.txt) do ren %%? " *.*"
for %%? in (00??????.txt) do ren %%? " *.*"
for %%? in (0???????.txt) do ren %%? " *.*"
lfnfor on
for %%? in (" *.txt") do ren "%%?" %%?
:: End of batch

--
Todd Vargo (double "L" to reply by email)



Posted by Todd Vargo on September 20th, 2004



"Todd Vargo" <toddvargo@alvantage.com> wrote in message
news:2r92m3F16kev6U1@uni-berlin.de...
OTOH, the OP is using Windows 2000, therefore, my original code should work
just fine for the OP.

--
Todd Vargo (double "L" to reply by email)



Posted by Leo on September 21st, 2004


"Todd Vargo" <toddvargo@alvantage.com> wrote in message news:<2r93clF17sqdpU1@uni-berlin.de>...
Thank you, Todd. What you sent worked like a champ. I did end up
remming out a few lines because I wanted to keep the filnames exactly
in 4.3 format. It was word for word like you originally wrote though.
I appreciate the help immensely and warn you now that I will come to
you, on this forum, for all manner of silly DOS questions.

Posted by Todd Vargo on September 21st, 2004



"Leo" <leonidesruiz@hotmail.com> wrote
Leo,

Now that we know your OS is W2K, please post further inquiries to
news:alt.msdos.batch.nt (which I read and post to as well). But more
importantly, that is the appropriate place where discussions of batch code
for NT/2K/XP systems belong.

--
Todd Vargo (double "L" to reply by email)




Similar Posts