When calling a
find /V /I
only with /V(!) the ERRORLEVEL is always 0 it appears to me. (Using Win7 x64)
If, for example, I run the following batch file
@echo off
SETLOCAL enabledelayedexpansion
find /V /I "camstart" testtest.txt
echo Errorlevel is !ERRORLEVEL!
on this testtest.txt file
intrinsicParamStatus(2338763)
calibrationFormatID(260)
calibrationFormatID(260)
leftCamStartX(88)
leftCamStartY(170)
rightCamStartX(88)
the output is:
---------- TESTTEST.TXT
intrinsicParamStatus(2338763)
calibrationFormatID(260)
calibrationFormatID(260)
Errorlevel is 0
and if I run
@echo off
SETLOCAL enabledelayedexpansion
find /V /I "camstarRt" testtest.txt
echo Errorlevel is !ERRORLEVEL!
the output is
intrinsicParamStatus(2338763)
calibrationFormatID(260)
calibrationFormatID(260)
leftCamStartX(88)
leftCamStartY(170)
rightCamStartX(88)
Errorlevel is 0
Evidently the output matches the task to find the string "camStarRt", which is not there, so it outputs all lines. But why doesn't that change the errorlevel?
When do exactly the same with
@echo off
SETLOCAL enabledelayedexpansion
find /I "camstarrt" testtest.txt
echo Errorlevel is !ERRORLEVEL!
The errorlevel becomes 1, as expected.
Is this a bug in find? How can I come across this?
My goal is to execute a task if a certain string in the output of a python-script is found. But if it's not, all lines of it shall be displayed as usual to see thework of the script.
python.exe C:\pycan-bin\xxx\yyy.py -m read -p lala16 -o %outfile% parameters.json | find /V /I "Response timeout"
find
will filter the lines that match a requested condition.
As some lines have passed the filter there is no reason to raise the errorlevel to signal that no matching lines have been found so it will be set to 0
errorlevel
will be raised (set to 1) only when find
does not echo anything, that is, there is no any line matching the request.
You can try with
@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
)
The basic idea is to execute the command inside a for /f
command. It will iterate over the ouput lines of the invoked python
. In each iteration the for
replaceable parameter will hold the line being processed.
This value is assigned to a variable to be able to use string substitution, replacing "Response timeout"
with nothing. If the variable with the substring removed is equal to the variable, the substring is not contained, else the substring is present and a variable (timeoutFound
) is defined to indicate it. Later the variable definition will be used to determine if the task needs to be execute.
When a block of commands is parsed before being executed (the case of the for
command), all variable read operations are removed, replaced with the value in the variables before block execution. But we need to assign a value to a variable inside the for
loop to do the substring operation. To deal with it, delayed expansion is needed (more here).