title: Windows Command Line Processor: Multiple and Nested IF Statements
body: <p>Intended software: windows command line processor (version 6.1.7601.17514)</p>
  
  <p>Hi,</p>
  
  <p>I've been trying to build a multiple-statement command line that runs within a short-cut. My goal is to be able to click one short-cut that checks if my hosted network is started or not, and then takes appropriate action based on the check. The code that starts and stops the hosted network is fine, and for the most part, the logic works, but I notice odd behavior when I check the outputs of the logic. I suspect that my problem has to do with the way I structured the statements, but I'm having difficulty properly interpreting the built-in documentation and the documentation I can find in the MSDN library. If it's possible, I want to avoid using batch files for this solution.</p>
  
  <p>To keep things simple, I've substituted my lengthy "netsh" commands with "echo" commands that show the errorcode. The code below is what I'm using to test my logic:</p>
  
  <p><strong>Test Code</strong></p>
  
  <pre><code>netsh wlan show hostednetwork | find "Not" &amp;&amp; echo found %errorlevel% || echo lost %errorlevel%
  </code></pre>
  
  <p>Currently, the way I'm reading this is:</p>
  
  <ol>
  <li>Show me hostednetwork's status and send the output to input</li>
  <li>Attempt to find the string "Not" in the input</li>
  <li>If the attempt succeeds, output "found" and the errorcode to the screen</li>
  <li>If the attempt fails, then output "lost" and the errorcode to the screen</li>
  </ol>
  
  <p>Notice that I'm not using any flags on the <code>find</code> command. I'm doing this because I want to reduce the chance of finding a false match. To clarify what I mean, I'll show the output if I just put in<br>
  <code>netsh wlan show hostednetwork</code>:</p>
  
  <p><strong>Sample Output of Hostednetwork Status</strong></p>
  
  <pre><code>C:\Windows\system32&gt;netsh wlan show hostednetwork
  
  Hosted network settings
  -----------------------
      Mode                   : Allowed
      SSID name              : "TestHost"
      Max number of clients  : 100
      Authentication         : WPA2-Personal
      Cipher                 : CCMP
  
  Hosted network status
  ---------------------
      Status                 : Not started
  </code></pre>
  
  <p>If I search for the string "Not", then that's sufficient to tell me that the hosteadnetwork is not started, because when the hosteadnetwork is started, the output shows "Started".</p>
  
  <p>The way I'm simulating the conditions of the hostednetwork is with the following commands:</p>
  
  <pre><code>netsh wlan start hostednetwork
  netsh wlan stop hostednetwork
  </code></pre>
  
  <p>I expect that when I open a command prompt (as an administrator):</p>
  
  <ol>
  <li>If the hostednetwork is not started, I should see a "found 0" in the output, meaning that the string was found and that there were no errors. </li>
  <li>If the hostednetwork is started, I should see a "lost 1" in the output, meaning that the string was not found and that there was an error.</li>
  </ol>
  
  <p>Case #1 works, but case #2 doesn't work on the first try. Here's my output when the hostednetwork is already started:</p>
  
  <p><strong>Output With Hostednetwork Started</strong></p>
  
  <pre><code>C:\Windows\system32&gt;netsh wlan start hostednetwork
  The hosted network started.
  
  
  C:\Windows\system32&gt;netsh wlan show hostednetwork | find "Not" &amp;&amp; echo found %er
  rorlevel% || echo lost %errorlevel%
  lost 0
  
  C:\Windows\system32&gt;netsh wlan show hostednetwork | find "Not" &amp;&amp; echo found %er
  rorlevel% || echo lost %errorlevel%
  lost 1
  </code></pre>
  
  <p><strong>Other Attempted Solutions</strong></p>
  
  <p>The way I've written the test code is the best I could come up with so far. In previous attempts, I've tried:</p>
  
  <ol>
  <li>Setting a custom variable instead of using the errorlevel variable, but I get the same output on case #2.</li>
  <li>Changing the code into an if else equivalent, but that didn't pan out very well.</li>
  <li>Wrapping the conditional statements in brackets "()" after the pipe and using different combinations of the special symbols "&amp;" and "|".</li>
  </ol>
  
  <p><strong>Other Questions</strong></p>
  
  <p>This question is related to another that I've been trying to figure out. If I wanted to search for three different strings in a command's output and exit on a different error code for each string, how can I do this? The syntax below is my starting point:</p>
  
  <pre><code>myCommand [/options] | ((find "string1" &amp;&amp; exit /b 2 || ver&gt;nul) &amp;&amp; 
  (find "string2" &amp;&amp; exit /b 3 || ver&gt;nul) &amp;&amp; (find "string3" &amp;&amp; exit /b 4 || ver&gt;nul))
  </code></pre>
  
  <p>For the same reasons above, I didn't use any flags on the "find" commands. Also, I used "ver>nul" in an attempt to keep the syntax correct since I know the "ver" operation succeeds.</p>
  
  <p>Any assistance is appreciated.</p>