Last token in batch variable

后端 未结 5 2042
死守一世寂寞
死守一世寂寞 2020-12-11 03:54

In a batch file, how can I get the last token in a variable regardless of whether it is a number or a word and regardless of how many tokens/words there are in the variable.

5条回答
  •  既然无缘
    2020-12-11 04:14

    Some code golf on Eli's answer.

    @echo off
    
    set var1=This is a String
    REM token_string will be manipulated each loop
    set token_string=%var1%
    
    :find_last_loop
    for /F "tokens=1*" %%A in ( "%token_string%" ) do (
      set last=%%A
      set token_string=%%B
      goto find_last_loop)
    
    echo %last%
    
    pause
    

    And to find something with ':' delimiters...

    for /F "tokens=1* delims=:" %%A in ( "%token_string%" ) do (
    

提交回复
热议问题