Since ERRORLEVEL is an environment variable isn’t it possible that its value can be changed before I get a chance to check it in my batch file?
Since ERRORLEVEL is an environment variable isn’t it possible that its value can be
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Environment variables belong to the current “process” so they can’t be changed from outside. Provided you check the error level after the relevant command in your batch file, you should be checking the right value.
You can confirm this by opening up two command windows and entering into the first:
and then the second:
then go back to the first and:
You should be very careful about setting the
errorlevelenvironment variable by the way. That particular variable is a special one in that, when you haven’t set it specifically, it will automatically serve up the return code from the previous program.Setting it explicitly overrides this behaviour, and the only way to recover is to either use
if errorlevel N(a) instead of the environment variable (it bypasses the environment variable), or useset errorlevel=to remove the override.(a) The correct way to do this is mandated by fact that the
errorlevelexpression is true if the error level is greater than or equal to the value specified. Because of that, you should do it in reverse order, something like: