PC Review


Reply
Thread Tools Rate Thread

Counter within FOR Loop

 
 
David Ashe
Guest
Posts: n/a
 
      12th Nov 2004
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%



 
Reply With Quote
 
 
 
 
Matthias Tacke
Guest
Posts: n/a
 
      12th Nov 2004
David Ashe schrieb:
> 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

--
Gruesse Greetings Saludos Saluti Salutations
Matthias
---------+---------+---------+---------+---------+---------+---------+
 
Reply With Quote
 
Paul R. Sadowski [MVP]
Guest
Posts: n/a
 
      12th Nov 2004
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].


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Do While Loop using time as the counter =?Utf-8?B?YXNtZW51dA==?= Microsoft Excel Programming 2 26th Sep 2006 11:58 PM
Counter variable in For Loop almostmature@gmail.com Microsoft Excel Programming 3 8th Jun 2006 06:56 PM
Should I use Do-While loop for my record counter? excelnut1954 Microsoft Excel Programming 0 24th Mar 2006 09:25 PM
Batch Counter in For-Do loop homerlex Microsoft Windows 2000 CMD Promt 1 30th Dec 2004 06:28 PM
On Screen Loop Counter simoncohen Microsoft Excel Programming 3 2nd Apr 2004 10:36 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:41 PM.