Search code examples
batch-filecmd

How to get last n tokens from string using "FOR /F" in batch file


for /f "tokens=2*" %%a in ('echo 111 222 333 444 555') do (
    echo %%a
)

How to get all tokens from 2 to n, if I do not know exact numbers of tokens in the string?


Solution

  • This should work:

    for /f "tokens=1,* delims= " %%a in ('echo 111 222 333 444 555') do (
        echo %%b
    )
    

    tokens=2,* works quite confusing. It doesn't mean that the tokens from the second to the last are stored in %%a but that %%a is the second and %%b is the rest. So if you want to get 222 333 444 555 in one variable you have to use tokens=1,*. This will put 111 into %%a and the rest into %%b.