用批处理文件比较字符串中的2个值

时间:2018-11-22 07:39:57

标签: batch-file compare

我有以下代码可获取一组许可证的当前状态:

set licensefile="D:\PLM\Solid Edge ST10\Preferences\SElicense.dat"
set _output=lmutil lmstat -c %licensefile% -f "solidedgeclassic"
for /f "tokens=2 delims=()=" %%G IN ('%_output% ^|find "issued"') DO set line=%%G

这会将%line%设置为等于以下字符串:

Total of 6 licenses issued;  Total of 5 licenses in use

当然,这些值可以更改,但是句子将始终相同。 如果5 >= 6,我希望批处理脚本返回错误。

我该怎么做?

编辑:_output语句的结果是:

lmutil - Copyright (c) 1989-2017 Flexera Software LLC. All Rights Reserved.
Flexible License Manager status on Thu 11/22/2018 09:05
[Detecting lmgrd processes...]
License server status: 27001@VIRT04
License file(s) on VIRT04: C:\SEFlex\Program\SELicense.dat:

VIRT04: license server UP v11.14.0

Vendor daemon status (on virt04):

 selmd: UP v11.14.0
Feature usage info:

Users of solidedgeclassic:  (Total of 6 licenses issued;  Total of 3 licenses in use)

  "solidedgeclassic" v110.0, vendor: selmd, expiry: permanent(no expiration date)
  floating license

    person1 PC-CAS016 PC-CAS016 (v110.0) (virt04/27001 1218), start Thu 11/22 8:00
    person2 PC-CAS09 PC-CAS09 (v110.0) (virt04/27001 599), start Thu 11/22 8:06
    person4 PC-CAS015 PC-CAS015 (v110.0) (virt04/27001 645), start Thu 11/22 8:51


Press any key to continue . . .

2 个答案:

答案 0 :(得分:0)

如果我确切了解您的数据是什么样子,可以这样做:

@echo off
set licensefile="D:\PLM\Solid Edge ST10\Preferences\SElicense.dat"
set _output=lmutil lmstat -c %licensefile% -f "solidedgeclassic"

for /f "tokens=6,11" %%i in ('%_output% ^|find "issued"') do (
    if %%j geq %%i (
       echo Error %%j larger or equal %%i
     ) else (
       echo Success %%j less than %%i
   )
)
pause

答案 1 :(得分:0)

我可能会使用以下代码从中提取感兴趣的行并从中提取两个数字,并将它们分别存储到变量issuedused中,因此表达式之间的确切位置括号,或者换句话说,前面的单词的数量无关紧要:

for /f "tokens=2 delims=()=" %%G IN ('
    %_output% ^| findstr /R /C:"(Total of [0-9][0-9]* licenses issued;  *Total of [0-9][0-9]* licenses in use)"
') do (
    for /F "tokens=3,8" %%I in ("%%G") do (
        set "issued=%%I" & set "used=%%J"
    )
)

例如,要输出错误消息并以ErrorLevel值为1终止批处理脚本,请执行以下操作:

if %used% geq %issued% (
    >&2 echo ERROR!
    exit /B 1
)