Volodymyr Bezuglyy
Volodymyr Bezuglyy

Reputation: 16795

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?

Upvotes: 2

Views: 1431

Answers (1)

MichaelS
MichaelS

Reputation: 6032

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.

Upvotes: 3

Related Questions