Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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?

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>

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%

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)

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%

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 felicityM2

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

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.

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.

The difference between a featuire and a bug is the feature has seniority.
=====================================
Helping others achieve escape felicityM2

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.

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.

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.

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.

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.

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.

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?"

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |