Name: denniss Date: December 28, 2007 at 08:53:10 Pacific Subject: XP Pro Batch Script -FOR loop issue OS: XP Pro CPU/Ram: Core 2 Duo/2 GB Model/Manufacturer: 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.
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?
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
The information on Computing.Net is the opinions of its users. Such
opinions may not be accurate and they are to be used at your own risk.
Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE