Last token in a batch variable

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 are in the variable.

I will pass the argument to the batch file as follows:

C:\execbatch.bat "This is example one" or C:\execbatch.bat "This is example number 2"

And then the batch file will look like this:

 @echo off SET var=%1 SET last=some command to grab the last token of %var% echo %last% exit 

Essentially, I need to be able to always grab the last token, in these examples I would like to grab one from Example 1 and 2 from Example 2.

ADDITIONAL INFORMATION:

These are not command line arguments, this is just a simplified example:

The fact is that I call the command inside the batch file and send its output to the variable as follows:

C:\execbatch.bat memory or C:\execbatch.bat cpu

And then in the batch file:

 For /F "Tokens=*" %%I in ('c:\monitor.exe -C custom_performance_counter -t %1 -w 80 -c 90') Do Set COMMANDOUTPUT=%%I Echo %COMMANDOUTPUT% Set CURRENTVALUE=some command to grab the last token of %COMMANDOUTPUT% Echo "The current value is %CURRENTVALUE%" 

This will result in different results depending on the type of check; however, in each case, the last token / variable is always the current value, although either a number or a word.

+6
batch-file
source share
5 answers
 @echo off set var1=This is a String set var2=%var1% set i=0 :loopprocess for /F "tokens=1*" %%A in ( "%var1%" ) do ( set /A i+=1 set var1=%%B goto loopprocess ) echo The string contains %i% tokens. for /F "tokens=%i%" %%G in ( "%var2%" ) do set last=%%G echo %last% pause 
+4
source share

Why is it so complicated?

The solution for a string in a variable is:

 set var1=This is a String for %%A in (%var1%) do set last=%%A 

echo last =% last%

Solution for the result of an external command:

 set xcmd=c:\monitor.exe -C custom_performance_counter -t %1 -w 80 -c 90 for /f "tokens=*" %%I in ('%xcmd%') do for %%A in (%%~I) do set last=%%A echo last=%last% 
+5
source share

If you insist on passing only one argument, you can use a routine:

 @echo off call :get_last %~1 echo.%last% goto :eof rem get_last tokens... :get_last set last=%1 shift if [%1]==[] goto :eof goto get_last 

However, if you just pass the number of pairs per packet, you can do this directly:

 @echo off :l set last=%1 shift if [%1]==[] goto pl goto l :pl echo %last% 

Side note. You rarely want to put exit in a batch file, since it exits from the shell rather than from the batch. This is annoying if you use it directly from the shell or from other batch files.

+1
source share

Some golf course at Eli is in charge.

 @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 find something with delimiters ':' ...

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

The shortest solution:

 @ECHO OFF SET var=This is a String SET Token=1 SET last= :Loop FOR /F "tokens=%Token%" %%i IN ("%var%") DO ( SET last=%%i SET /A Token+=1 GOTO Loop ) ECHO The last token is: "%last%" PAUSE > NUL 
-1
source share

All Articles