Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

XP Pro Batch Script -FOR loop issue

Original Message
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.

Any suggestions are welcome.

Thank you.

dss


Report Offensive Message For Removal


Response Number 1
Name: denniss
Date: December 28, 2007 at 12:38:56 Pacific
Subject: XP Pro Batch Script -FOR loop issue
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 2
Name: denniss
Date: December 28, 2007 at 13:29:04 Pacific
Subject: XP Pro Batch Script -FOR loop issue
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 3
Name: Mechanix2Go
Date: December 29, 2007 at 02:34:27 Pacific
Subject: XP Pro Batch Script -FOR loop issue
Reply: (edit)
"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



Report Offensive Follow Up For Removal




Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: XP Pro Batch Script -FOR loop issue

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




XP Installed to G?

exessive internet traffic

ZoneAlarm Question. Blocked Connect

Windows Live Messenger Problem

Delete $Uninstall after SP3 updates


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

All content ©1996-2007 Computing.Net, LLC