Computing.Net > Forums > Programming > XP Pro Batch Script -FOR loop issue

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

XP Pro Batch Script -FOR loop issue

Reply to Message Icon

Name: denniss
Date: December 28, 2007 at 08:53:10 Pacific
OS: XP Pro
CPU/Ram: Core 2 Duo/2 GB
Product: Dell Lattitude D830
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: denniss
Date: December 28, 2007 at 12:38:56 Pacific
Reply:

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


0

Response Number 2
Name: denniss
Date: December 28, 2007 at 13:29:04 Pacific
Reply:

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 :EOF

seems to work...!!!


dss


0

Response Number 3
Name: Mechanix2Go
Date: December 29, 2007 at 02:34:27 Pacific
Reply:

"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



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


%%~Za wierdness Javascript- inner Java



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: XP Pro Batch Script -FOR loop issue

Batch file FOR LOOP www.computing.net/answers/programming/batch-file-for-loop/11504.html

Nested loops in batch scripting www.computing.net/answers/programming/nested-loops-in-batch-scripting/12050.html

Grand Total of variables in a loop www.computing.net/answers/programming/grand-total-of-variables-in-a-loop/15996.html