Counter within FOR Loop

D

David Ashe

I am trying to take lines from a file, and split the contents into 4 files,
first line to the first file, 2nd to the second, etc. but the fifth line
goes back into the first file. (Actually, just the 4th token, not the whole
line) I am using a counter (creatively named "counter") to decide where the
line goes however, I am getting unexpected results. I Expect to get a
round-robin of VALUE:1, VALUE:2,..., and the final "End Counter" being
between 1 and 4. However, The Final End Counter is equal to the number of
lines in the file, and I get VALUE:1, over and over.

It is definitely some kind of scope issue, but I don't know how to fix it.

------- Begin Script ---------------
set COUNTER=1

for /f "tokens=1-4" %%A in (%1) do (
if %COUNTER% EQU 1 (
echo VALUE:1

) else if %COUNTER% EQU 2 (
echo VALUE:2

) else if %COUNTER% EQU 3 (
echo VALUE:3

) else if %COUNTER% EQU 4 (
echo VALUE:4
set COUNTER=1
)
set /a COUNTER+=1

)
echo.
echo End Counter: %COUNTER%
 
M

Matthias Tacke

David said:
I am trying to take lines from a file, and split the contents into 4 files,
first line to the first file, 2nd to the second, etc. but the fifth line
goes back into the first file. (Actually, just the 4th token, not the whole
line) I am using a counter (creatively named "counter") to decide where the
line goes however, I am getting unexpected results. I Expect to get a
round-robin of VALUE:1, VALUE:2,..., and the final "End Counter" being
between 1 and 4. However, The Final End Counter is equal to the number of
lines in the file, and I get VALUE:1, over and over.

It is definitely some kind of scope issue, but I don't know how to fix it.
Hello David.

Variables in areas enclosed in parenthes are evaluated by the shell
when entering this area. To overcome this behaviour you have to use
delayedexpansion (see setlocal /? or cmd /?) or when your os is nt4
which doesn't have delayed expansion you can use a call to an
internal sub or a pseudo call with a set or echo statement following.


::CounterMod4.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
setlocal EnableDelayedExpansion
set COUNTER=0
for /f "tokens=1-4" %%A in (%1) do (
set /a " COUNTER+=1, Val=(COUNTER-1)%%4+1"
echo VALUE: !Val!
call echo VALUE: %%Val%%
)
echo.End Val : %Val%
echo End Counter: %COUNTER%
::CounterMod4.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::

HTH
 
P

Paul R. Sadowski [MVP]

Hello, David:
On Fri, 12 Nov 2004 14:16:16 -0800: you wrote...

DA>
DA> It is definitely some kind of scope issue, but I don't know how to fix
DA> it.

Another case for delayedexpansion:
@echo off
setlocal enableextensions enabledelayedexpansion
set x=1
:loop
if !x! gtr 10 goto :EOF
echo !x!
set /a x = x + 1
goto loop


DA>
DA> ------- Begin Script ---------------
DA> set COUNTER=1
DA>
DA> for /f "tokens=1-4" %%A in (%1) do (
DA> if %COUNTER% EQU 1 (
DA> echo VALUE:1
DA>
DA> ) else if %COUNTER% EQU 2 (
DA> echo VALUE:2
DA>
DA> ) else if %COUNTER% EQU 3 (
DA> echo VALUE:3
DA>
DA> ) else if %COUNTER% EQU 4 (
DA> echo VALUE:4
DA> set COUNTER=1
DA> )
DA> set /a COUNTER+=1
DA>

Regards, Paul R. Sadowski [MVP].
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top