0

I’m trying to setup a section of my script to test for specific ERRORLEVEL results and respond to the user accordingly.

I have the following that tests for what seems to be the error

-2147024891 (Access is Denied)

when trying to modify a file but it never triggers even though the !ERRORLEVEL! on the line before reports the -2147024891 error.

CALL :TEE ERROR - ERROR# !ERRORLEVEL! occured on %me% - please check event logs for further details
IF !ERRORLEVEL! EQU -2147024891 (
    CALL :TEE ERROR - Please make sure to run this script from an elevated command prompt
)

I have SETLOCAL enableDelayedExpansion set therefore I am using !ERRORLEVEL! rather than %ERRORLEVEL% - but I am still a little unclear how delayedexpansion subtly changes the behavior of the script.

I understand the best way to check for specific numbers is to use the following but I am not sure how to do this for negative numbers.

IF ERRORLEVEL <N> IF NOT ERRORLEVEL <N+1> <COMMAND>

Any suggestions?

CristiFati
  • 38,250
  • 9
  • 50
  • 87
Javagenki
  • 9
  • 3
  • `errorlevel` gets reset to 0 every time a command is successful. Therefore after `call tee` errorlevel is 0. – Klitos Kyriacou Sep 14 '16 at 15:37
  • On _Win10_ command interpreter, it works the same with negative integers. – CristiFati Sep 14 '16 at 15:59
  • 1
    Are you sure that the subroutine at `:TEE` label ends with `exit /B -2147024891`? – Aacini Sep 14 '16 at 16:10
  • 1
    @KlitosKyriacou: Not exactly. `call` command does not modify the ERRORLEVEL nor `exit /B` command. Some internal commands modify the ERRORLEVEL, but some others not. Full details at [this answer](http://stackoverflow.com/questions/34987885/what-are-the-errorlevel-values-set-by-internal-cmd-exe-commands/34987886#34987886) – Aacini Sep 14 '16 at 16:18
  • @Aacini thanks for the correction. I think what might be happening is some command inside the :TEE subroutine might be changing the errorlevel. I presume the errorlevel is first set by some command before calling :TEE (and :TEE probably prints out its parameters to the screen and adds it to a log), rather than by :TEE itself. – Klitos Kyriacou Sep 14 '16 at 16:41
  • Thanks! I did some more testing and it was the 'call' that was resetting errorlevel. I don't suppose there is any way around that? – Javagenki Sep 14 '16 at 17:40
  • Thanks! I did some more testing and it was the `call` - or something about that action - that was resetting errorlevel. When I removed the `call` from the first line the IF statement was executed. I don't suppose there is any way around that? – Javagenki Sep 14 '16 at 17:50
  • The `:TEE` subroutine must preserve the errorlevel. This can be easily done if its first line is something like `set value=%errorlevel%` and its last line is `exit /B %value%`. – Aacini Sep 14 '16 at 18:31
  • 1
    ... although a simpler way would be to save the errorlevel in a variable _before_ the `call :TEE` and use this value after the `call`... – Aacini Sep 14 '16 at 18:37
  • Perfect - thanks, Aacini. – Javagenki Sep 14 '16 at 20:46

0 Answers0