Batch ERRORLEVEL results different from CMD?

Why does ERRORLEVEL behave differently in these two circumstances?

From the command line:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>aescrypt.exe -v 2> NUL

C:\>echo %errorlevel%
9009

Versus from batch file:

@echo off

set /P C="> "?

set or=
if "%C%"=="a" set or=1
if "%C%"=="A" set or=1
if defined or (
    aescrypt.exe -v 2> NUL
    echo %errorlevel%
)

Result:

> a
1
This question and answers originated from www.stackoverflow.com
Question by (10/14/2010 6:53:14 PM)

Answer

Remove you "@echo off" and see how the code is being executed. You might find that the errorlevel in example 2 is the result of the "if defined".

Also, try this:

@echo off
set /P C="> "?
set or=
if /i "%C%"=="a" set or=1
if not defined or goto SKIP
aescrypt.exe -v 2> NUL
echo %errorlevel%
:SKIP

Answer by

Find More Answers
Related Topics  batch  errorlevel
Related Questions
  • Why does cmd.exe have different errorlevel behavior on a 64-bit machine?

    If I make a batch script named temp.bat (for example) containing: exit /b 1 When I run it in various ways, I get different behavior on my 32-bit XP system vs. a 64-bit XP system. On 32-bit…
  • Windows batch-file errorlevel question

    I've got a batch file that parses a bunch of file names out of a text file and concatenates them into a single strong - it was previously discussed here . However, I don't want the string to contain…
  • ERRORLEVEL inside IF

    Just stumbled into a weird thing with %ERRORLEVEL% and wanted to see if anyone knows why and if there's a way to fix it. Essentially, it seems as if commands executed inside if statements don't set …
  • Errorlevel in a For loop (batch windows)

    I have the following windows batch code: for %%i in (iidbms iigcc iigcd dmfacp dmfrcp rmcmd qwerty) do ( tasklist | findstr /i %%i echo %errorlevel% if %errorlevel% == 0 (echo %…
  • TortoiseSVN from the command line and "IF ERRORLEVEL"?

    I have a batch file I'm running from a Windows XP w/service pack 3 workstation which applies SQL changes to a database using sqlcmd.exe in SQL 2005. I've got a command-line entry for TortoiseSVN …
  • Why does makensis.exe return errorlevel 1 even though it worked?

    I have a NSIS script that works. It compiles, the produced installer works fine. And yet, makensis.exe returns 1 instead of 0. This is a real pain because I use it in a continuous integration setup …
  • Create event with the batch files

    Here's what i want to achieve. We have this email archive database which we optimize on a weekly basis . At the moment we are manually logging in run the command and monitor the status. The optimiza…
  • Killing a process in Batch and reporting on success

    i have the following batch file, which terminates the iTunes program so, that if i connect my iPod, it's not going to sync it. (I know you can set this up in iTunes.) @echo off :kill cls taskkill…
  • get return code from plink?

    In a DOS batch script, I'm running a single command on a remote (also windows) computer using plink. Formerly, this command was only run on the local machine, and was relying on the return code to d…
  • How to set ERRORLEVEL in SSIS?

    I have a batch file that runs an SSIS job. I have no knowledge of how the SSIS job runs, I took over a project involving it. The batch file uses %ERRORLEVEL% to detect errors that occur within t…