Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Programming (http://www.go4expert.com/articles/programming/)
-   -   Passing more than 26 -31 tokens via FOR in DOS (http://www.go4expert.com/articles/passing-26-31-tokens-via-dos-t25268/)

Cleptography 17Mar2011 08:03

Passing more than 26 -31 tokens via FOR in DOS
 
I have seen all over the net limitations with the FOR command in DOS and passing more than 26 - 31 tokens and the work around. Here is one.

I have come up with my own method for doing this and as of yet have not seen anything resembling it, so I will post it. Instead of using a single string I counted all tokens within an entire file, but the same method could be used on a single line or string.

Text File
Code:

Hello world this is the first string
Hello world this is the second string
Hello world this is the third string
World this is not enough strings
Counting how many tokens are in this file
Oh look we can go above 26 28 32 with very little code
I am amused at how simple this is
Wow lets keep counting tokens
Gosh if we did this with a string and not an entire file imagine the posibilities
How many tokens have we counted so far...
86

Script
Code:

@echo off
 setlocal

set file=%~1
set token=1

for /f "tokens=*" %%- in (%file%) do (
    set str=%%-&&call :NEXT
)
goto :eof

:NEXT
for /f "tokens=1,*" %%a in ("%str%") do (
    echo.[%%a] is the %token% token
    set str=%%b
)
set /a token=%token%+1
if "%str%"=="" goto :eof
goto :NEXT

Notes

This was done with less than 20 lines of code minus all blank spaces and putting the FOR command on single lines shortens it up to 11 lines total. This method will count an infinite amount of tokens.
THE END =P


All times are GMT +5.5. The time now is 21:06.