Computing.Net > Forums > Programming > For loop info needed

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.

For loop info needed

Reply to Message Icon

Name: T.C.
Date: October 20, 2009 at 18:59:11 Pacific
OS: Windows XP HE SP.3+
Subcategory: Batch
Comment:

Cmd.exe batch scripting.

There seems to be a limit on the number of Tokens in a For loop of 31, is this so and can it be increased by any method?



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: October 20, 2009 at 22:16:21 Pacific
Reply:

C:\Users>for /?
Runs a specified command for each file in a set of files.

. . . .

    %i is explicitly declared in the for statement and the %j and %k
    are implicitly declared via the tokens= option.  You can specify up
    to 26 tokens via the tokens= line, provided it does not cause an
    attempt to declare a variable higher than the letter 'z' or 'Z'.
    Remember, FOR variables are single-letter, case sensitive, global,
    and you can't have more than 52 total active at any one time.

 . . . .

C:\Users>


0

Response Number 2
Name: T.C.
Date: October 21, 2009 at 00:56:33 Pacific
Reply:

Thankyou, yes I've read that but it doesn't quite fit the facts.

All of the ASCII printable characters from ! (char 33) thru' ~ (char 126) can be used as variables although some must be Escaped. In the following script 31 tokens are specified and Ascii chars 91 thru' 95 are used to extract columns from the .csv file which is created in the first few lines of the script.

I've been unable to expand the tokens past max 31 hence the query.

@echo off
cls
setlocal enabledelayedexpansion 

for /l %%a in (1,1,80) do (
    set column=!column!Col.%%a,
)

set column=!column:~0,-1!
echo !column!>%temp%\column.csv

for /f "tokens=1-30* delims=," %%A in (%temp%\column.txt) do (
    set column01-30=%%A,%%B,%%C,%%D,%%E,%%F,%%G,%%H,%%I,%%J,%%K,%%L,%%M,%%N,^
%%O,%%P,%%Q,%%R,%%S,%%T,%%U,%%V,%%W,%%X,%%Y,%%Z,%%[,%%\,%%],%%^^
    set remainder=%%_
)
del %temp%\column.csv
echo %column01-30%
echo.
echo %remainder%


0

Response Number 3
Name: nbrane
Date: October 21, 2009 at 01:05:59 Pacific
Reply:

if you call another batchfile, will it inherit the constraints
prevailing on the calling batchfile? (f/e: if you've used all
31 tokens in batchfile "a", and you call batchfile "b", will
batchfile "b" already used up 31 tokens or start from 0).
If not, maybe try calling another batchfile, sending the
current posn. in csv line, and "start from scratch". batchfile names might be
variable by iincrementation of integer applied to batchname,
if needing more than one sub-batch to do the job (ie, more
than 32x2)


0

Response Number 4
Name: T.C.
Date: October 21, 2009 at 01:36:08 Pacific
Reply:

Nbrane Thank you for your input. At this stage I don't know if calling a second .bat file and "handing on" the current position is feasible but doubt it very much. Will investigate that another time.

In order to progress into the csv record another For loop can be used but this time using the output written to the remainder variable as below. This allows editing of fields in a csv record regardless of how many fields there are, but I want (if possible) to increase the number of Tokens which can be used. The following script shows an example of Ascii characters being used as variables.

@echo off
cls
setlocal enabledelayedexpansion 

:: Create csv file..
for /l %%a in (1,1,80) do (
    set column=!column!Col.%%a,
)
set column=%column:~0,-1%
echo %column%>%temp%\column.csv

:: Extract leading 30 columns..
for /f "tokens=1-30* delims=," %%^& in (%temp%\column.csv) do (
    set column01-30=%%^&,%%',%%(,%%^),%%^*,%%+,%%,,%%-,%%.,%%/,%%0,%%1,%%2,^
%%3,%%4,%%5,%%6,%%7,%%8,%%9,%%:,%%;,%%^<,%%=,%%^>,%%?,%%@,%%A,%%B,%%C
    set remainder=%%D
)

:: Extract columns 31 thru 60
for /f "tokens=1-30* delims=," %%^& in ("%remainder%") do (
    set column31-60=%%^&,%%',%%(,%%^),%%^*,%%+,%%,,%%-,%%.,%%/,%%0,%%1,%%2,^
%%3,%%4,%%5,%%6,%%7,%%8,%%9,%%:,%%;,%%^<,%%=,%%^>,%%?,%%@,%%A,%%B,%%C

    set remains=%%D
)

del %temp%\column.csv

echo %column01-30%
echo.
echo %column31-60%
echo.
echo %remains%


0

Response Number 5
Name: Mechanix2Go
Date: October 21, 2009 at 03:47:14 Pacific
Reply:

I did a little tinkering. [w2k sp4] and the batch interpreter won't use a number greater than 31 for tokens.

Oddly, it will use a 2 char var:

for /f "tokens=1-31 delims=," %%1 in (column.csv) do (
echo %%22
)

LOL

I think it boils down to using a sub to get tokens out past 31.

====================================

@echo off > newfile & setLocal enableDELAYedexpansion

for /f "tokens=* delims=," %%a in (column.csv) do (
  call :sub1 %%a
)

goto :eof

:sub1
  :loop
  echo %1
  shift
  if "%1" neq "" goto :loop
goto :eof


=====================================
Helping others achieve escape felicity

M2


1

Related Posts

See More



Response Number 6
Name: klint
Date: October 21, 2009 at 10:31:26 Pacific
Reply:

This calls a sub for each line in the CSV file. The sub then uses a goto-loop to echo one value from the comma-separated list in each iteration. (The for loop just separates the first value from the rest of the list.) I have a habit of using exit /b instead of goto :eof because it's more flexible, you can return an error code with exit /b if you want.

for /f "delims=" %%L in (columns.csv) do (
   call :parseLine %%L
)
exit /b

:parseLine
setlocal
set v=%1

:loop
for /f "tokens=1* delims=," %%a in ("%v%") do (
   echo %%a
   set v=%%b
)
if not "%v%" == "" goto :loop
endlocal
exit /b


0

Response Number 7
Name: Razor2.3
Date: October 21, 2009 at 14:31:03 Pacific
Reply:

Maybe it's time for a different language? Just sayin'.


0

Response Number 8
Name: T.C.
Date: October 21, 2009 at 15:15:16 Pacific
Reply:

Mechanix2Go The ability to refer to a field by its token number is another undocumented feature which makes batch editing of csv files soooo much easier. There are several others which I'm investigating, unfortunately as yet I haven't found any references to these undocumented features.

Klint Thank you for your input, most welcome.

Razor2.3 Point taken but the topic leans heavily on whether or not the required outcome can be achieved using batch scripting.

Again - thank you all.


0

Response Number 9
Name: Razor2.3
Date: October 22, 2009 at 05:53:49 Pacific
Reply:

is another undocumented feature
Know the difference between a bug and a feature? Documentation.

My point? MS only guarantees 26 tokens per FOR loop, 52 per instance of CMD. It might work across all versions, but maybe not.


0

Response Number 10
Name: Mechanix2Go
Date: October 22, 2009 at 06:02:27 Pacific
Reply:

The difference between a featuire and a bug is the feature has seniority.


=====================================
Helping others achieve escape felicity

M2


0

Response Number 11
Name: T.C.
Date: October 22, 2009 at 18:52:43 Pacific
Reply:

Know the difference between a bug and an undocumented feature?

The bug is unintentional, the undocumented feature is an intentional enhancement.

MS only guarantees 26 tokens per FOR loop, 52 per instance of CMD.

MS only documemts these, there just ain't no guarantee.


0

Response Number 12
Name: Razor2.3
Date: October 22, 2009 at 19:16:37 Pacific
Reply:

MS only documemts these, there just ain't no guarantee.
Right, and the gas peddle in my car isn't guaranteed to be on the right side, it's just where the documentation says it should be.


0

Response Number 13
Name: T.C.
Date: October 23, 2009 at 01:45:26 Pacific
Reply:

Right, and the gas peddle in my car isn't guaranteed to be on the right side, it's just where the documentation says it should be

But you could have it moved to any position you like, or have a second gas pedal fitted, and it then becomes an undocumented feature, an intentional enhancement for a person who can't hack having the gas pedal on the right. Anyone reading the manual for your model of vehicle would believe that the gas pedal was to the right until they did a bit of "Can we" instead of "We can't".

However, I doubt it we'd ever agree except to disagree. The undocumented enhancements are available and I will be making full use of them until such time as MS might decide to remove them (doubt if MS will ever allocate funds for this purpose), at which time I will use VB or C++.

Take care.


0

Response Number 14
Name: Razor2.3
Date: October 23, 2009 at 15:04:51 Pacific
Reply:

until they did a bit of "Can we" instead of "We can't".
It wasn't a question of "can/cannot," it's a question of "should/should not."
Yes, I can keep my engine red-lined, but that doesn't mean I should. It might explode, either literally, or figuratively.

I doubt it we'd ever agree except to disagree.
Agreed. I'm just glad I'm the one in the health care industry.


0

Response Number 15
Name: klint
Date: October 24, 2009 at 11:57:54 Pacific
Reply:

Razor: "Agreed. I'm just glad I'm the on in the health care
industry."

I bet T.C. now comes back and says he's in the nuclear power
industry. No, God forbid, no.


0

Response Number 16
Name: T.C.
Date: October 25, 2009 at 00:38:12 Pacific
Reply:

LOL. We seem to have gone a bit Off-Topic.

I cannot claim any direct affiliation with the nuclear industry but, as his service record shows, as a very young sailor my maternal grandfather was witness to the A-bomb tests at the Bikini Atoll and I once visited a nuclear powered submarine at its base in Scotland. Is that good enough?

If I get a job as a cleaner at Bellevue will that qualify me as being "in the health care industry"?

Meantime I'll carry on as a lowly but reasonably well-paid office manager for a legal/accountancy partnership.

Amen.


0

Response Number 17
Name: Razor2.3
Date: October 25, 2009 at 05:24:21 Pacific
Reply:

If I get a job as a cleaner at Bellevue will that qualify me as being "in the health care industry"?
Depends. Do your unchecked mistakes lead to "undocumented features," or do they lead to "pending lawsuits?"


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: For loop info needed

FOR loops in C www.computing.net/answers/programming/for-loops-in-c/8449.html

Two for loops at the same time? www.computing.net/answers/programming/two-for-loops-at-the-same-time/14085.html

C++ - need FOR loop help www.computing.net/answers/programming/c-need-for-loop-help/9209.html