Computing.Net > Forums > Programming > variables in batch FOR loops

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

variables in batch FOR loops

Reply to Message Icon

Original Message
Name: oldog
Date: April 6, 2008 at 20:51:31 Pacific
Subject: variables in batch FOR loops
OS: win xp pro sp2
CPU/Ram: c2duo/3gb
Model/Manufacturer: dell
Comment:

Hi all! Probably a silly question, but I'm rather new to batch programming and the following just doesn't make sense to me at all!


@ECHO OFF
SETLOCAL
SET CNTR=0
FOR %%i IN (one,two,three) DO (
SET /a CNTR=CNTR+1
ECHO %CNTR%
)
ECHO %CNTR%
GOTO :EOF


I'm expecting the output to show
1
2
3
3

but I get
0
0
0
3

There's obviously something I don't get about defining and using variables... Any help appreciated!


Report Offensive Message For Removal


Response Number 1
Name: oldog
Date: April 6, 2008 at 21:33:45 Pacific
Reply: (edit)

... OK don't worry, I got it! (EnableDelayedExpansion)

sorry for wasting space!


Report Offensive Follow Up For Removal

Response Number 2
Name: Mechanix2Go
Date: April 7, 2008 at 06:46:43 Pacific
Reply: (edit)

For simplicity, you can skip onotialing the var.

And increment like this:

::==
@echo off
setLocal EnableDelayedExpansion

for %%a in (one two three) do (
set /a cntr+=1
echo !cntr!
)
echo !cntr!


=====================================
If at first you don't succeed, you're about average.

M2


Report Offensive Follow Up For Removal







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








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge
Poll History




Data Recovery Software