Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 批处理文件将findstr输出的数字放入IF语句的变量中?_Batch File_Findstr - Fatal编程技术网

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

Batch file 批处理文件将findstr输出的数字放入IF语句的变量中?,batch-file,findstr,Batch File,Findstr,我有一个日志文件,其中相关部分分别与findstr隔离,如下所示: 22 removed 0 updated 数字会发生变化,前面有空格,我只想将数字放入变量中,这样我就可以将它们与阈值进行比较,并以此作为继续还是停止脚本的基础 SET removed_threshold=100 SET updated_threshold=100 基本上,我希望脚本只在日志文件中的两个数字都低于100时继续 问题是我被困在一开始,因为我不可能把数字变成变量。在类似问题中尝试在for/f中

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

   22 removed
   0 updated
数字会发生变化,前面有空格,我只想将数字放入变量中,这样我就可以将它们与阈值进行比较,并以此作为继续还是停止脚本的基础

SET removed_threshold=100    
SET updated_threshold=100
基本上,我希望脚本只在日志文件中的两个数字都低于100时继续


问题是我被困在一开始,因为我不可能把数字变成变量。在类似问题中尝试在for/f中使用findstr不起作用,它找不到findstr,甚至找不到完整路径的文件。

My findstr Com并仅从日志文件中选择包含关键字
的行,该关键字被删除
更新
之前至少有一位数字
[0-9][0-9]*

带有默认分隔符空格的
for/f“tokens=1,2”
将数字存储在
%%A
中,将找到的关键字存储在
%%B

这用于命令
set“%%B\u current=%%A”

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

:: Q:\Test\2019\02\28\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_

请记住,我们不在您的电脑上,我们只有您发布的信息。我们没有足够的信息来构建一个可以复制您的问题的环境,如果我们不能做到这一点,我们只能猜测。我建议您发布一段您拥有的代码和日志文件中的足够内容(或者最好是输出到日志文件的命令),以便我们开始帮助您。显示您的findstr命令,我建议将其包含在一个
for/f
中,以直接解析输出并使用当前值设置变量。非常感谢!它工作完美无瑕。这有点超出我的理解力,但我会查找我没有得到的部分。谢谢你的反馈,我详细阐述了一些工作原理。
> Q:\Test\2019\02\28\SO_54928501.cmd
both values below treshold
removed_current=22
removed_threshold=100
updated_current=0
updated_threshold=100