Errorlevel and Findstr

Errorlevel and Findstr

  • Comments 2

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.
)
Leave a Comment
  • Please add 7 and 8 and type the answer here:
  • Post
  • 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_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /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

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

    reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName | findstr /c:"Windows 7" >NUL

    IF NOT ERRORLEVEL 1 (

    echo I found Windows 7.

    ) else (

    echo I did not find Windows 7.

    )

Page 1 of 1 (2 items)
Search