For loop info needed

October 20, 2009 at 18:59:11
Specs: Windows XP HE SP.3+
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?


See More: For loop info needed

Report •


#1
October 20, 2009 at 22:16:21
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>


Report •

#2
October 21, 2009 at 00:56:33
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%


Report •

#3
October 21, 2009 at 01:05:59
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)

Report •

Related Solutions

#4
October 21, 2009 at 01:36:08
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%


Report •

#5
October 21, 2009 at 03:47:14
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


Report •

#6
October 21, 2009 at 10:31:26
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


Report •

#7
October 21, 2009 at 14:31:03
Maybe it's time for a different language? Just sayin'.

Report •

#8
October 21, 2009 at 15:15:16
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.


Report •

#9
October 22, 2009 at 05:53:49
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.


Report •

#10
October 22, 2009 at 06:02:27
The difference between a featuire and a bug is the feature has seniority.


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

M2


Report •

#11
October 22, 2009 at 18:52:43
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.


Report •

#12
October 22, 2009 at 19:16:37
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.

Report •

#13
October 23, 2009 at 01:45:26
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.


Report •

#14
October 23, 2009 at 15:04:51
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.


Report •

#15
October 24, 2009 at 11:57:54
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.


Report •

#16
October 25, 2009 at 00:38:12
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.


Report •

#17
October 25, 2009 at 05:24:21
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?"

Report •


Ask Question