Search code examples
windowscmderrorlevel

Can Windows CMD.EXE ever return an %ERRORLEVEL% of -1?


I'm under the impression that the errorlevel provided back to command-line level scripting is exactly the value provided by a windows application to its Exit(..) call. See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682658%28v=vs.85%29.aspx

Presumably, an exit call might be called with "-1" as an argument, in spite of the fact that the error code would seem to be an unsigned number.

Does that value get through to a calling command script? (Not that I want it. I need a value which is clearly not a valid error number, and isn't zero).


Solution

  • @ECHO OFF
    SETLOCAL
    CALL :negerr 1
    ECHO ERRORLEVEL = %errorlevel%
    GOTO :EOF
    
    :negerr
    EXIT /b -%1
    

    You mean like this?