批处理文件将 findstr 输出中的数字放入 IF 语句的变量中?

Batch file putting numbers from a findstr output into variables for IF statement?

我有一个日志文件,其中的相关部分分别用 findstr 隔离,如下所示:

   22 removed
   0 updated

数字发生变化并且在它们前面有空格,我只想将数字放入变量中,以便我可以将它们与阈值进行比较,并以此作为继续或停止脚本的基础。

SET removed_threshold=100    
SET updated_threshold=100

基本上我希望脚本仅在日志文件中的两个数字都低于 100 时才继续。

问题是我在一开始就卡住了,因为我不可能将数字放入变量中。尝试在类似问题的 /f 中使用 findstr 不起作用,即使有完整路径也找不到 findstr 或文件。

我的 findstr 命令仅从日志文件中选择包含关键字 removedupdated 且前面至少有一位数字 [0-9][0-9]*.

的行

带有默认分隔符space(忽略前导分隔符)的for /f "tokens=1,2"将数字存储在%%A中,将找到的关键字存储在%%B

这个用于命令 set "%%B_current=%%A"

为了确保变量确实被设置,在比较不带引号的值之前,首先需要清除并检查它们。

:: Q:\Test19\SO_54928501.cmd
@Echo off
SET "Logfile=.\SO_54928501.Logfile"

:: clear vars
for %%A in (removed_ updated_) do for /f "delims==" %%B in ('
    Set %%A 2^>Nul
') do Set "%%B="

SET "removed_threshold=100"
SET "updated_threshold=100"

:: extract lines and set vars
For /f "tokens=1,2" %%A in ('
    findstr /I "[0-9][0-9]*.removed [0-9][0-9]*.updated" ^<"%Logfile%"
') do set "%%B_current=%%A"

if not defined removed_current (Echo removed_current not set & exit /B 1)
if not defined updated_current (Echo updated_current not set & exit /B 2)
if %removed_current% geq %removed_threshold% (
    Echo removed_current above treshold & exit /B 3)
if %updated_current% geq %updated_threshold% (
    Echo updated_current above treshold & exit /B 4)

Echo both values below treshold
set removed_
set updated_

带有上述日志数据的示例输出:

> Q:\Test19\SO_54928501.cmd
both values below treshold
removed_current=22
removed_threshold=100
updated_current=0
updated_threshold=100