Computing.Net > Forums > Windows NT > Math in a batch file (NT)

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Math in a batch file (NT)

Reply to Message Icon

Name: Robin Siebler
Date: June 26, 2003 at 13:41:00 Pacific
OS: Win2k
CPU/Ram: N/A
Comment:

I am trying to increment a counter as I loop through a section of a batch file. I tried using 'set /a' and incrementing a variable, but I could not figure out how to echo the value of the variable. Can someone tell me how to do this?



Sponsored Link
Ads by Google

Response Number 1
Name: Robin Siebler
Date: June 26, 2003 at 13:48:51 Pacific
Reply:

After quite a bit of searching, I managed to find the answer to my question:

Environment variable that get evaluated within a FOR loop get expanded only at the beginning of the loop. So the following snippet does not produce the expected result:

set count=1
FOR /f "tokens=*" %%a in ('dir /b') do (
echo %count%: %%a
set /a count+=1)


To force the variable to get evaluated at the proper iteration, use the new subroutine call mechanism to take the evaluation outside of the loop.

set count=1
FOR /f "tokens=*" %%a in ('dir /b') do (
call :exec echo %%count%%: %%a
set /a count+=1)
goto :EOF

:exec
%*
goto :EOF



0
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Windows NT Forum Home


Sponsored links

Ads by Google


Results for: Math in a batch file (NT)

using a WAIT command in a batch file www.computing.net/answers/windows-nt/using-a-wait-command-in-a-batch-file/15784.html

ending a task in a batch file www.computing.net/answers/windows-nt/ending-a-task-in-a-batch-file/11427.html

How to run a Batch file in a Batch file www.computing.net/answers/windows-nt/how-to-run-a-batch-file-in-a-batch-file/12244.html