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.
batch-file
Eli
source share