Errorlevel and Findstr


ErrorLevel is not %ERRORLEVEL% . This is probably the first one you should read.

Next is the usage of the ERRORLEVEL statement. http://support.microsoft.com/kb/69576

 

The following table shows the effect of statement when writing your batch scriipt.

Statement Algebraic Equivalent.
IF ERRORLEVEL 5 … IF E = 5 OR E > 5 THEN …
IF NOT ERRORLEVEL 6 IF E < 6 THEN …

Here is a sample using findstr and error level. Findstr returns 0 if it successfully finds any occurrence.

findstr -sip Failed log.txt > NULL
IF NOT ERRORLEVEL 1 ( 
    echo Found.
) else (
    echo Not Found.
)

Comments (2)

  1. Scott Walker says:

    Using your method this returns true that it finds Windows 7 on Windows 8 and Windows 2008 OS's?

    Can you explain where I am going wrong?

    reg query "HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows NTCurrentVersion" /v ProductName | findstr /i "Windows 7 professional" >NUL

    IF NOT ERRORLEVEL 1 (

       echo I found Windows 7.

    ) else (

       echo I did not find Windows 7.

    )

    pause

  2. Sajay says:

    Scott findstr needs /c:"…" for an exact match. Change the script to this worked for me .

    reg query "HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows NTCurrentVersion" /v ProductName | findstr /c:"Windows 7" >NUL

    IF NOT ERRORLEVEL 1 (

    echo I found Windows 7.

    ) else (

    echo I did not find Windows 7.

    )