title: If conditional in batch (inside a for, inside an if)
body: <p>I have an application that reads through all the files in the directory and will rename those files (if necessary) and create log files from the innards of them, however there are a couple of catches....</p>
  
  <ol>
  <li>If the files 15th character is something other than a "." or "_", substring the 14 characters previous plus the 15th and store as variable.</li>
  <li>If the files 15th character is a "." or "_" substring the 14 characters previous and store as variable.</li>
  </ol>
  
  <p>For example, if filename is: DIY_0000000000a.xml the variable would be DIY_0000000000a and if the filename is: DIY_0000000000.xml the variable would be DIY_0000000000</p>
  
  <p>There is a catch to all of this...</p>
  
  <p>If the user picks Option 2, which is to rerun the application, then the application should rename all files in the directory to DIY_xxx_rerun.xml or DIY_xxxa_rerun.xml (should now explain the "_" in the aforementioned options. </p>
  
  <p>The problem I have and what I need assistance with is that I can't seem to get the conditional logic correct. The application works, but if I have a mix of files in the directory (ie: DIY_0000000000a.xml, DIY_0000000000.xml &amp; DIY_0000000000_rerun.xml, DIY_0000000000a_rerun.xml) it may work on one kind but not the other. Help is much appreciated.</p>
  
  <p>Below is a snippet of my current code:</p>
  
  <pre><code>SETLOCAL EnableDelayedExpansion
  
      :EXECUTE
      ::IF AN XML FILE EXISTS ADD IT TO A LOG FILE, PROMPT USER IF THEY WANT TO VIEW IT
      ECHO.
      ECHO =============================================
      ECHO **SELECT YOUR OPTION BY USING YOUR KEYBOARD**
      ECHO =============================================
      ECHO.
      ECHO 1  - Generate DIY Log Files for normal orders
      ECHO 2  - Generate DIY Log Files for _rerun orders
      ECHO 3  - EXIT
  
      ECHO.
      ECHO.
      SET /P CHOICE=Type your option, then press ENTER:
      IF %CHOICE%==1 (
          SET RERUN=
          GOTO PROCESS
      )
      IF %CHOICE%==2 (
          SET RERUN=_rerun
          GOTO PROCESS
      )
      IF %CHOICE%==3 EXIT
      IF NOT '%CHOICE%'== SET CHOICE=%CHOICE:~0,1%
      ECHO.
      ECHO "%CHOICE%" is not a valid option.
      PAUSE
      GOTO EXECUTE
  
      :PROCESS
      IF EXIST DIY*.xml (
          SET /a FILECOUNTER=0
          ECHO.
          ECHO Processing...
          FOR /f "delims=" %%a IN ('DIR /b /a-d /on DIY*.xml') DO (
              SET /a LOGCOUNTER=!FILECOUNTER!/1+1
              SET ORDERNUMBER=%%a
              IF "!ORDERNUMBER:~14,1!" NEQ "." (
                  IF "!ORDERNUMBER:~14,1!" NEQ "_" (
                  SET ORDERNUMBSTR=!ORDERNUMBER:~0,15!
                  )   ELSE (
                          SET ORDERNUMBSTR=!ORDERNUMBER:~0,14!
                  )
              )
              SET ORDERNUMBSTR=!ORDERNUMBER:~0,14!
              IF "%RERUN%"=="_rerun" (
                  REN !ORDERNUMBSTR!.xml !ORDERNUMBSTR!%RERUN%.xml &gt;NUL 2&gt;&amp;1
              )   ELSE (
                  REN *!ORDERNUMBSTR!*.xml !ORDERNUMBSTR!.xml &gt;NUL 2&gt;&amp;1
              )
              FOR /f "tokens=4 delims=&lt;&gt;" %%i IN ('TYPE !ORDERNUMBSTR!%RERUN%.xml ^|find "DIY_LOG_ID"') DO (
                  SET DIYLOGID=%%i
              )
              FOR /f "tokens=7 delims=&lt;&gt;" %%j IN ('TYPE !ORDERNUMBSTR!%RERUN%.xml ^|find "ACCOUNT_KEY"') DO (
                  SET ACCOUNTKEY=%%j
                  FOR /f "tokens=1-3 delims=-" %%x IN ("!ACCOUNTKEY!") DO (
                      SET CONTRACTNR=%%x
                      SET PLANSEQ=%%y
                      SET SUBSEQ=%%z
                  )
              )
  ...
  
  ENDLOCAL
  </code></pre>