title: Getting the exit code of an application started with the "cmd" and "start" commands
body: <p>I have a console application. Interaction with this application is done via TCP/IP. </p>
  
  <p>I also have a test framework for it, which is basically a collection of BATCH scripts (...not my fault). What this test framework does for each test is basically this: </p>
  
  <ol>
  <li><code>start /min "myapplication.exe"</code> and wait until verification is received that the application is up and running.</li>
  <li>send commands via TCP/IP to this application, receive its replies, and check if the timings and values agree with whatever is expected by the specific test. </li>
  </ol>
  
  <p>One problem that I'm currently encountering is that the application exits prematurely due to some internal error. I would like to distinguish between failed tests, and the application crashing. The one and only indication I have for this, is the application's exit code. </p>
  
  <p>So, I tried the following: </p>
  
  <p><code>start /min cmd /c "myapplication.exe || echo %errorLevel% &gt; exitcode.txt"</code></p>
  
  <p>and then later on in the test scripts, </p>
  
  <pre><code>if exist exitcode.txt (
      set /p exitcode=&lt;exitcode.txt
      echo ERROR: myapplication.exe returned exitcode %exitcode%.
      goto error
  ) else (
      goto do_processing
  )
  </code></pre>
  
  <p>but for some strange reason, the text file never appears, even though I sometimes get a dialog about the application crashing, and even though I forcibly make it fail with a known non-zero exit code. The test just goes through <code>do_processing</code> and (of course) results in failure. </p>
  
  <p><strong>EDIT</strong>
  When I run </p>
  
  <p><code>start /min cmd /c "nonsense || echo %errorLevel% &gt; test.txt"</code></p>
  
  <p>I <em>sometimes</em> get a text file containing the string 9009, but other times that text file contains the string <code>0</code>, or sometimes <code>1</code>, ...What the...?!</p>
  
  <p><strong>EDIT2</strong>
  If you type </p>
  
  <p><code>cmd /k "nonsense || echo %errorLevel%"</code></p>
  
  <p>(note the <code>/k</code> option), you see <code>0</code> being printed in the new window, but if you then type <code>echo %errorlevel%</code>, you get <code>1</code>....</p>
  
  <p>I knew batch was not very sane, but it should at least be <em>consistently insane</em>...</p>
  
  <p>Any ideas on what could be going on here? </p>