最后令牌批次变量 [英] Last token in batch variable
问题描述
在一个批处理文件,我怎样才能在变量中最后一个令牌不管它是否是一个数字或一个词,不管有多少令牌/词中有变量。
我将传递一个参数到批处理文件是这样的:
C:\\ execbatch.bat这是例如,一个
要么 C:\\ execbatch.bat这是例子2号
和则该批处理文件将是这样的:
关闭@echoSET VAR =%1
最后设置=一些命令抢到%VAR%的最后一个令牌
回声%,去年%
出口
从本质上讲,我需要能够始终抓住最后一个令牌,在这些例子中,我会想抓住有一个
从例子1和 2
从例子2
其他信息:
这不是命令行参数的问题,这仅仅是一个简单的例子:
的情况是,我打电话命令一个批处理文件内,将其输出发送给一个变量,像这样:
C:\\ execbatch.bat内存
或 C:\\ execbatch.bat CPU
然后在批处理文件:
FOR / F令牌= *%%我在('C:\\ MONITOR.EXE -C custom_performance_counter -t%1 -w 80 -c 90')不要设置COMMANDOUTPUT = %%我回声%COMMANDOUTPUT%
设置CurrentValue的一些=命令抢到%COMMANDOUTPUT%的最后一个令牌
echo当前值为%CurrentValue的%
这将输出取决于检查类型不同的结果;然而,在任何情况下的最后一个令牌/变量始终尽管无论是数字或单词的当前值。
关闭@echo集VAR1 =这是一个字符串
设置VAR2 =%VAR1%
集I = 0:loopprocess
FOR / F令牌= 1 *%%的(%VAR1%)在做(
集/ A I + = 1
设置VAR1 = %%乙
转到loopprocess)呼应字符串包含%I%令牌。FOR / F令牌=%I%%%的G(%VAR2%)并设定最后= %%摹回声%,去年%暂停
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.
@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
这篇关于最后令牌批次变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!