Passing more than 26 -31 tokens via FOR in DOS

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

  1. Cleptography

    Cleptography New Member

    Joined:
    Sep 2, 2010
    Messages:
    39
    Likes Received:
    7
    Trophy Points:
    0
    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
     

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