Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

DOS - If NOT statements

Options
  • 30-07-2004 2:20pm
    #1
    Registered Users Posts: 1,531 ✭✭✭


    I'm doing some DOS scripting, and the HELP IF command suggests that one can do the following:
    IF [NOT] ERRORLEVEL number command

    This would suggest to me, that I could do whatever jiggerypokery I wanted in a script, then at the end of each dodgy command do a "IF NOT ERRORLEVEL 0 pause" which would only do a 'pause' command if an error was encountered.

    However, this doesn't seem to work. If I open a new prompt. Then do say "cd xxxxxxxx", which doesn't exist. Then do an "echo %ERRORLEVEL%" it does indeed show a non zero value as I would expect (and doing the echo again does retain the errorlevel). But trying the above command (IF NOT ERRORLEVEL 0 pause), I don't get the pause I would expect.

    There are other ways to do this of course, but this is probably the neatest if it would work...


Comments

  • Closed Accounts Posts: 92 ✭✭tempest


    You've just encountered one of the most strange dos issues ever.....

    if errorlevel 0 will return true if errorlevel is greater than or equal to 0 rather than if errorlevel is equal to 0.

    not will flip the balance so therefore if not errorlevel 0 will return true if errorlevel is less than 0, therefore the script will only pause if there is a negative value for errorlevel.

    what you want is

    if %errorlevel% neq 0 pause


  • Registered Users Posts: 1,531 ✭✭✭Drakar


    Thanks.
    if not %errorlevel%==0 pause
    also works. I was just somewhat confused as to why something that looked logically correct was misbehaving. The possibility that Microsoft might have coded something that doesn't work as one might expect hadn't ever entered into my (or I'm sure any other sane person's) mind. Shame on you.


Advertisement