find /v 的错误级别始终为 0
errorlevel of find /v is always 0
调用
时
find /V /I
只有 /V(!) 在我看来,ERRORLEVEL 始终为 0。 (使用 Win7 x64)
例如,如果我运行下面的批处理文件
@echo off
SETLOCAL enabledelayedexpansion
find /V /I "camstart" testtest.txt
echo Errorlevel is !ERRORLEVEL!
在这个 testtest.txt 文件上
intrinsicParamStatus(2338763)
calibrationFormatID(260)
calibrationFormatID(260)
leftCamStartX(88)
leftCamStartY(170)
rightCamStartX(88)
输出为:
---------- TESTTEST.TXT
intrinsicParamStatus(2338763)
calibrationFormatID(260)
calibrationFormatID(260)
Errorlevel is 0
如果我 运行
@echo off
SETLOCAL enabledelayedexpansion
find /V /I "camstarRt" testtest.txt
echo Errorlevel is !ERRORLEVEL!
输出是
intrinsicParamStatus(2338763)
calibrationFormatID(260)
calibrationFormatID(260)
leftCamStartX(88)
leftCamStartY(170)
rightCamStartX(88)
Errorlevel is 0
显然输出与查找字符串 "camStarRt" 的任务相匹配,但字符串 "camStarRt" 不存在,因此它输出所有行。但为什么这不改变错误级别?
何时与
完全相同
@echo off
SETLOCAL enabledelayedexpansion
find /I "camstarrt" testtest.txt
echo Errorlevel is !ERRORLEVEL!
如预期的那样,错误级别变为 1。
这是 find 中的错误吗?
我怎么会遇到这个?
我的目标是在 python 脚本的输出中找到特定字符串时执行任务。但如果不是,它的所有行将照常显示以查看脚本的工作。
python.exe C:\pycan-bin\xxx\yyy.py -m read -p lala16 -o %outfile% parameters.json | find /V /I "Response timeout"
find
将过滤符合请求条件的行。
由于某些行已通过过滤器,因此没有理由提高错误级别以表示未找到匹配行,因此它将被设置为 0
errorlevel
仅在 find
不回显任何内容时才会引发(设置为 1),即没有任何行匹配请求。
你可以试试
@echo off
setlocal enableextensions disabledelayedexpansion
set "timeoutFound="
for /f "delims=" %%a in ('
python.exe C:\pycan-bin\xxx\yyy.py -m read -p lala16 -o %outfile% parameters.json
') do (
set "line=%%a"
setlocal enabledelayedexpansion
if not "!line:Response timeout=!"=="!line!" set "timeoutFound=1"
echo(!line!
for %%b in ("!timeoutFound!") do endlocal & set "timeoutFound=%%~b"
)
if defined timeoutFound (
rem Do Something
)
基本思路是在for /f
命令中执行命令。它将遍历调用 python
的输出行。在每次迭代中,for
可替换参数将保留正在处理的行。
这个值被分配给一个变量,以便能够使用字符串替换,用什么替换 "Response timeout"
。如果去掉子串的变量等于变量,则不包含子串,否则存在子串并定义一个变量(timeoutFound
)来表示。稍后将使用变量定义来确定是否需要执行任务。
当一个命令块在执行前被解析时(for
命令的情况),所有变量读取操作都被删除,替换为块执行前变量中的值。但是我们需要在for
循环里面给一个变量赋值来做子串操作。为了应对,需要延迟扩容(more here).
虽然find /?
帮助中没有具体描述这一点,但是errorlevel值的标准是“命令成功为0,失败为1或更多”,而在[=12的情况下=],它 returns 只有当它不 find/display 一行时才为 1。
在您的前两个示例中:find /V /I "camstart" testtest.txt
和 find /V /I "camstarRt" testtest.txt
结果应该相同,因为 /I 开关指示处理 "camstart" 和 "camstaRt" 和 "camStart" 和 "CamStart" 字符串相同。
如果在输出中找到 "camstart" 字符串(忽略大小写),下面的代码“执行任务。但如果没有,它的所有行将照常显示以查看脚本的工作."
find /I "camstart" testtest.txt > linesFound.txt
if !errorlevel! equ 0 (
type linesFound.txt
execute the task here
) else (
type testtest.txt
)
调用
时find /V /I
只有 /V(!) 在我看来,ERRORLEVEL 始终为 0。 (使用 Win7 x64)
例如,如果我运行下面的批处理文件
@echo off
SETLOCAL enabledelayedexpansion
find /V /I "camstart" testtest.txt
echo Errorlevel is !ERRORLEVEL!
在这个 testtest.txt 文件上
intrinsicParamStatus(2338763)
calibrationFormatID(260)
calibrationFormatID(260)
leftCamStartX(88)
leftCamStartY(170)
rightCamStartX(88)
输出为:
---------- TESTTEST.TXT
intrinsicParamStatus(2338763)
calibrationFormatID(260)
calibrationFormatID(260)
Errorlevel is 0
如果我 运行
@echo off
SETLOCAL enabledelayedexpansion
find /V /I "camstarRt" testtest.txt
echo Errorlevel is !ERRORLEVEL!
输出是
intrinsicParamStatus(2338763)
calibrationFormatID(260)
calibrationFormatID(260)
leftCamStartX(88)
leftCamStartY(170)
rightCamStartX(88)
Errorlevel is 0
显然输出与查找字符串 "camStarRt" 的任务相匹配,但字符串 "camStarRt" 不存在,因此它输出所有行。但为什么这不改变错误级别?
何时与
完全相同@echo off
SETLOCAL enabledelayedexpansion
find /I "camstarrt" testtest.txt
echo Errorlevel is !ERRORLEVEL!
如预期的那样,错误级别变为 1。
这是 find 中的错误吗? 我怎么会遇到这个?
我的目标是在 python 脚本的输出中找到特定字符串时执行任务。但如果不是,它的所有行将照常显示以查看脚本的工作。
python.exe C:\pycan-bin\xxx\yyy.py -m read -p lala16 -o %outfile% parameters.json | find /V /I "Response timeout"
find
将过滤符合请求条件的行。
由于某些行已通过过滤器,因此没有理由提高错误级别以表示未找到匹配行,因此它将被设置为 0
errorlevel
仅在 find
不回显任何内容时才会引发(设置为 1),即没有任何行匹配请求。
你可以试试
@echo off
setlocal enableextensions disabledelayedexpansion
set "timeoutFound="
for /f "delims=" %%a in ('
python.exe C:\pycan-bin\xxx\yyy.py -m read -p lala16 -o %outfile% parameters.json
') do (
set "line=%%a"
setlocal enabledelayedexpansion
if not "!line:Response timeout=!"=="!line!" set "timeoutFound=1"
echo(!line!
for %%b in ("!timeoutFound!") do endlocal & set "timeoutFound=%%~b"
)
if defined timeoutFound (
rem Do Something
)
基本思路是在for /f
命令中执行命令。它将遍历调用 python
的输出行。在每次迭代中,for
可替换参数将保留正在处理的行。
这个值被分配给一个变量,以便能够使用字符串替换,用什么替换 "Response timeout"
。如果去掉子串的变量等于变量,则不包含子串,否则存在子串并定义一个变量(timeoutFound
)来表示。稍后将使用变量定义来确定是否需要执行任务。
当一个命令块在执行前被解析时(for
命令的情况),所有变量读取操作都被删除,替换为块执行前变量中的值。但是我们需要在for
循环里面给一个变量赋值来做子串操作。为了应对,需要延迟扩容(more here).
虽然find /?
帮助中没有具体描述这一点,但是errorlevel值的标准是“命令成功为0,失败为1或更多”,而在[=12的情况下=],它 returns 只有当它不 find/display 一行时才为 1。
在您的前两个示例中:find /V /I "camstart" testtest.txt
和 find /V /I "camstarRt" testtest.txt
结果应该相同,因为 /I 开关指示处理 "camstart" 和 "camstaRt" 和 "camStart" 和 "CamStart" 字符串相同。
如果在输出中找到 "camstart" 字符串(忽略大小写),下面的代码“执行任务。但如果没有,它的所有行将照常显示以查看脚本的工作."
find /I "camstart" testtest.txt > linesFound.txt
if !errorlevel! equ 0 (
type linesFound.txt
execute the task here
) else (
type testtest.txt
)