title: Batch - Delayed expansion doesn't work with surrounding if clause
body: <p>In the following script I call a subroutine with a string, the maximal length of a substring of that string and a 3rd non-existing variable to get the substring back.</p>
  
  <p>The script should check if the next character after the string with the maximal substrings length is a space and if yes, then cut the string by that space (delete space to) and returning the substring and change the passed string by cutting the substring part. Example:</p>
  
  <p>string: "Hello World Bla"<br>
  substringLength: 5</p>
  
  <p><code>CALL :get_substring string substringLength substring</code> </p>
  
  <p>=> string: "World Bla" (no space), substringLength: 5 (no change), substring: "Hello"</p>
  
  <p>This works fine without if clause but it doesn't when i use an if clause, even if i use delayed expansion.</p>
  
  <p>Here is the working code without if statement:</p>
  
  <pre><code>@ECHO OFF
  SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
      SET string=Hello World wasserschutzpolizei
      SET /A substringLength=5
      CALL :get_substring string substringLength substring
      ECHO !string!
      ECHO !substring!
      EXIT /B 0
  ENDLOCAL
  
  :get_substring
      SETLOCAL ENABLEDELAYEDEXPANSION
  
      SET "string=!%~1!"
      SET "substringLength=!%2!"
      SET nextChar=!string:~%substringLength%,1!
  
      REM IF "!nextChar!"==" " (
          SET substring=!string:~0,%substringLength%!
          ECHO !substring!
          SET /A cutSpaceCount=!substringLength!+1
          SET string=!string:~%cutSpaceCount%!
  
          ECHO !string!
  
          ENDLOCAL &amp; SET %1=%string% &amp; SET %3=%substring% &amp; EXIT /B 0
      REM ) ELSE (
      REM     Some other case    
      REM )
  
  EXIT /B 0
  </code></pre>
  
  <p>This doesn't work when I comment in the if statement:</p>
  
  <pre><code>IF "!nextChar!"==" " (
      SET substring=!string:~0,%substringLength%!
      ECHO !substring!
      SET /A cutSpaceCount=!substringLength!+1
      SET string=!string:~%cutSpaceCount%!
  
      ECHO !string!
  
      ENDLOCAL &amp; SET %1=%string% &amp; SET %3=%substring% &amp; EXIT /B 0
  ) ELSE (
      REM Some other case    
  )
  </code></pre>
  
  <ol>
  <li>Why does the script doesn't work with an if-statement?</li>
  <li>How can fix it?</li>
  </ol>
  
  <p>Be aware that my routine should also include an else statement which I cutted out because the problem is the same.</p>