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
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
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)
>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.
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"
> Here's how I do that using 4DOS:
... or simply use the %@replace function to replace leading zeroes with
nothing....
--
Mit freundlichem Gruß,
Klaus Meinhard
It is probably a bad idea, at least of the number of leading zeroes
varies. Wherever possible, numeric fields should be right-aligned.
>What exact OS?
Since this is news:alt.msdos.batch, you should presume that the OS is
MSDOS..Win98/ME.
>Pure batch or are external programs possible?
>Are ther always 4 zero's?
>Is the file name always 8 places?
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.
*** 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/
>Michael Bednarek wrote:
>
>> Here's how I do that using 4DOS:
>
>... or simply use the %@replace function to replace leading zeroes with
>nothing....
@REPLACE will replace more than just the *leading* zeros! @LTRIM
might be a better choice.
--
Charles Dye ras...@highfiber.com
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.
>That's why I didn't use @REPLACE. I didn't use @LTRIM because I don't
>think it's part of 4DOS
Hmmm. You're right, it's not. I stand corrected.
--
Charles Dye ras...@highfiber.com
You seem to be using unreasonable definitions of "remove" and/or "work".
Only tested solutions are trustworthy.
Feel free to test the code to your hearts content.
Prove yourself trustworthy by doing your own testing before commenting.
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.
Depending on the OS solutions may differ heavily.
The proper group for nt/w2k/xp questions is news:alt.msdos.batch.nt
>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.
@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
>Leo wrote:
>>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.
>
>Depending on the OS solutions may differ heavily.
>The proper group for nt/w2k/xp questions is news:alt.msdos.batch.nt
>
>>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.
>
Or a ready solution:
::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
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" .
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
--
OTOH, the OP is using Windows 2000, therefore, my original code should work
just fine for the OP.
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.
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.