Remove leading zeros from a string

June 30, 2009 at 10:58:08
Specs: Windows XP
Hi,
1)
I have a string TRL000000012, I need to remove TRL and all the leading zeros, how can I do that?
The length is fixed 9 char so for example,

string this is what i need
TRL000000012 12


so will need the number 12,etc

2)
Also, how will I add leading zeros. The length remains 9 char and need to add zeros.

This is what I have
FOR /F "TOKENS=1" %%y in ('type %NEWFILE%') DO SET /A R=R+1

This say give 3 counts in the file and need to 000000009

Thanks


See More: Remove leading zeros from a string

Report •


#1
June 30, 2009 at 15:04:46
Hi B1pal

Where is this string in a var or in a file
and is %NewFile% a filename or the string


Report •

#2
June 30, 2009 at 19:04:15
Hi, for 1) It is in a file. It is the last line in a file.

set tail=!tail:~3!
:loop
if !tail:~0^,1! equ 0 set tail=!tail:~1^,9!&& goto :loop
echo !tail!


I got the above code working but it only works if the script is reading one file. I have 2 read two or more file then it does not work. I don't know what && does. It never reads the second file.

for 2) NEWFILE is the file name.


Report •

#3
July 1, 2009 at 09:38:32
HI B1pal

This Help

@echo off
SetLocal EnableDelayedExpansion

:Main
rem Get last line into a variable
for /f "tokens=* delims=" %%a in (~.txt) do (
set Data=%%a
)
echo %Data%

rem Get String Length
call :StrLen %Data%
echo String Length=%Len%

rem Del Leading Zeros
call :DelZeros %Data% %Len%
echo Num=%Num%

rem Add Leading Zeros
call :StrLen %Num%
echo Number Length=%Len%
call :AddZeros %Len% 9
echo Number=%Num%
exit /b

:StrLen %*
set Len=0
set Data=%*
for /L %%a in (0,1,80) do (
set Char=!Data:~%%a,1!
if not "!Char!"=="" set /a Len+=1
)
exit /b


:DelZeros %1 %2
set Data=%1
set Len=%2
set Flag=0
set Num=
for /L %%a in (3,1,!Len!) do (
set Char=!Data:~%%a,1!
if not !Char!==0 set Flag=1
if !Flag!==1 set Num=!Num!!Char!
)
exit /b


:AddZeros %1
set Len=%1
set Zeros=%2
set /a NoZeros=%Zeros%-%Len%
for /L %%a in (1,1,!NoZeros!) do (
set Num=0!Num!
)
exit /b


Report •

Related Solutions

#4
July 1, 2009 at 10:39:19
Hi,

This is not working and may be I didn't ask Q properly.

In StrLen:

:StrLen %*
set Len=0
set Data=%*
for /L %%a in (0,1,80) do (
set Char=!Data:~%%a,1!
if not "!Char!"=="" set /a Len+=1

What is this doing => for /L %%a in (0,1,80)

I already know the number of rows in the file. Say for example, there are 12 rows in a file. The total rowcount length is 9. So I will need to add 7 zeros.
000000012.

I think if I understand the StrLen then deleting zeros would be resolved as well.
Thanks for your help.


Report •

#5
July 1, 2009 at 15:02:46
Hi B1pal

StrLen, Just calulates the length of the string.

Is each line of your file a simlar string ie TRL000000012. If they are all the same length :StrLen is'nt needed.
Do you want to strip the TRL and all the zeros and then what,add them back on.
I thought TRL000000012 was only on the last line of the file



Report •

#6
July 7, 2009 at 12:31:25
Hi,

The file has several rows and the last line will be the Trailer line. So it will have depending on how many rows the file has will look like like this TRL000000015 (has 15 rows in the
file).

I can strip off the TRL and the zeros by doing this

set tail=!tail:~3!
:loop
if !tail:~0^,1! equ 0 set tail=!tail:~1!&& goto :loop

and it works. But since from this batch program it needs to read more than one file, the second file never get to read it and that is where I am stuck. Here is the whol program:

:: chk csv for today's date in header, chk num records in tail

@echo off & setLocal enableDELAYedexpansion

:today
set today=%DATE:~10,4%
set today=%today%%DATE:~4,2%
set today=%today%%DATE:~7,2%

echo today is !today!

echo.

:main
for /f "tokens=* delims= " %%a in ('dir/b *.txt') do (
set input=
echo %%a
set input=%%a

:count lines; set T & R
for /f "tokens=* delims= " %%d in ('find /v /c "" ^< %%a') do (
set T=%%d
set /a R=!T!

)

:chk contents of one csv & set head & tail
set N=0
for /f "tokens=* delims= " %%i in (%%a) do (
set /a N+=1
if !N! equ 1 set head=%%i
if !N! equ !T! set tail=%%i

set tail=!tail:~3!
:loop
if !tail:~0^,1! equ 0 set tail=!tail:~1!&& goto :loop

)


: chk date and record cout from head and tail
if !head:~3^,8! equ !today! echo %%a current ok
(if !tail! equ !R! echo record count OK
(if !head:~3^,8! equ !today! (
if !tail! equ !R! (
call :noFIRST
call :chopLAST
))
)
)
echo.
)
:end chk contents of one csv & set head & tail
)
:end main
goto :eof

:noFIRST
::== noFIRST.bat
@echo off > NewViasinc1.txt


set FIRST=Y
for /f "tokens=*" %%L in (!input!) do call :1 %%L
goto :eof

:1
if %FIRST%==N echo %*>>NewViasinc1.txt
set FIRST=N

goto :eof


:chopLAST
::==chopLAST.bat

find /v "" NewViasinc1.txt > ~Temp.txt
for /f "tokens=1,* delims=[]" %%a in (~Temp.txt) do set Last=%%a
type NewViasinc1.txt | find /v "%Last%" >> NewViasinc2.txt
copy NewViasinc2.txt !input!

del New*.txt
del ~Temp.txt
echo !input!
move !input! c:\Amex\batch\processing\Viasinc
set input=
set R=
set T=
goto :eof

::== DONE

goto :eof


Report •

#7
July 8, 2009 at 08:30:41
Hi b1pal

Change all the "Goto :eof" to "exit /b".
Calling a subroutine and ending in goto :eof will do that and
end the batch file.
The last goto :eof is pointless as it's already at the end of file.


Report •

#8
July 8, 2009 at 11:10:38
thanks!

Report •


Ask Question