Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have a FOR loop in a Batch Script on XP Pro.
FOR /L %%A IN (1 1 %engine_instance%) DO (
type %logs_dir%\sim_engine_%%A.log | FIND "Customers Processed This Run" > %logs_dir%\run_sim_temp_%%A.txt
if errorlevel 1 goto FAIL
for /F "tokens=1 delims= " %%Z in (%logs_dir%\run_sim_temp_%%A.txt) do (
set /A tot_accounts+=%%Z
)
)The problem I have is when the FOR is executed only once the tot_accounts variable is incorrect.
Example when 10 is expected I get 8. I have initialized this variable to 0 at the beginning of the script and it is never used anywhere before this FOR loop.
When the FOR loop is executed more than once the value is correct. So 5 and 5 for two loops gives 10. For one loop of 10, I see 8.When I echo the value before the FOR loop it correctly shows as 0.
Any suggestions are welcome.
Thank you.
dss

ok, i may have a clue now.
It appears that the %%Z value has leading zeros. Apparently this is a problem only the very first time tot_accounts is calculated. I manually removed the leading zeros from the text file before the FOR was executed and I got the correct value.
Which brings up the next question - How do I remove leading zeros from the value before I use it for calculations?
dss

ok, i seem to be replying to my own post..!!!
I got a little more creative here:
changed my FOR loop a bit:
FOR /L %%A IN (1 1 %engine_instance%) DO (
type %logs_dir%\sim_engine_%%A.log | FIND "Customers Processed This Run" > %logs_dir%\run_sim_temp_%%A.txt
if errorlevel 1 goto FAIL
for /F "tokens=1 delims= " %%Z in (%logs_dir%\run_sim_temp_%%A.txt) do (
call :CALC_TOTALS %%Z
)
)and added this:
:CALC_TOTALS
set /a temp_accounts=1%1 - 1000000
set /a tot_accounts+=%temp_accounts%
goto :EOFseems to work...!!!
dss

"ok, i seem to be replying to my own post..!!!"
:)
the problem with leading zeros is that SET /A treats the num as octal. [Anybody's guess why.]
I use this sub to 'unPAD' a num.
::==
:unPAD
:loop1
if '!str:~0^,1!'=='0' set str=!str:~1,99!& goto :loop1
goto :eof
=====================================
If at first you don't succeed, you're about average.M2

![]() |
%%~Za wierdness
|
Javascript- inner Java
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |