Passing more than 26 -31 tokens via FOR in DOS

Discussion in 'Programming' started by Cleptography, Mar 17, 2011.

  1. Cleptography

    Cleptography New Member

    Sep 2, 2010
    Likes Received:
    Trophy Points:
    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
    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...
    @echo off
    set file=%~1
    set token=1
    for /f "tokens=*" %%- in (%file%) do (
        set str=%%-&&call :NEXT
    goto :eof
    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

    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

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice