Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Last token in batch variable

Tags:

batch-file

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.

I will be passing an argument into the batch file like this:

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

And then the batch file will be 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 want to grab one from example 1 and 2 from example 2.

ADDITIONAL INFORMATION:

It is not the command line arguments that are the issue, that was just a simplified example:

The case is that I am calling a command inside of a batch file and sending its output to a variable like so:

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 output various results depending on the check type; however in every case the last token/variable is always the current value albeit either number or word.

like image 250
Eli Avatar asked Mar 29 '11 14:03

Eli


2 Answers

Why so complicated?

Solution for a string in a variable:

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%
like image 119
foo2 Avatar answered Nov 07 '22 08:11

foo2


@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
like image 6
Eli Avatar answered Nov 07 '22 09:11

Eli