title: How to create folder based on first and second part of file name and move files into the folder?
body: <p>I created the following batch script to create a folder based on today's date and then group files into folders based on the file name. </p>
  
  <p>For example the files</p>
  
  <pre><code>JIM_BRICKMAN_QPS.avi
  JIM_BRICKMAN_Slice.avi
  JIM_BRICKMAN_Slice.jpg
  </code></pre>
  
  <p>are moved to the folder <code>BRICKMAN</code>.</p>
  
  <p>This works fine, however, attempts to modify the batch file to move the newly created folders into the newly created date folder fail or overwrite the folders when going through the loop.</p>
  
  <pre><code>for /F "tokens=1-4 delims=/" %%A in ('date /t') do (
      set DateDay=%%A
      set DateMonth=%%B
      set DateYear=%%C
  )
  set CurrentDate=%DateDay%-%DateMonth%-%DateYear%
  if not exist "%CurrentDate%" md %CurrentDate%
  
  for %%A in (*.avi *.jpg) do (
      for /f "tokens=1,* delims=_" %%D in ("%%~nA") do (
          md "%%D" 2&gt;nul
          echo Moving file %%A to folder %%D
          move "%%A" "%%D" &gt;nul
      )
  )
  
  echo Finished
  </code></pre>
  
  <p>Additionally, I can't seem to get the token to ignore the first delimiter so that the folder is titled <code>JIM_BRICKMAN</code> and not just <code>BRICKMAN</code>.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I rewrote the batch file after the suggestions in the comments:</p>
  
  <pre><code>set "CurrentDate=%DATE:~-10,2%-%DATE:~-7,2%-%DATE:~-4%"
  if not exist "%CurrentDate%" md %CurrentDate%
  for %%A in (*.avi *.jpg) do (
      for /f "tokens=1,2 delims=_" %%D_%%E in ("%%~nA") do (
          md "%%D_%%E" 2&gt;nul
          move "%%A" "%%D_%%E" &gt;nul
      )
  )
  </code></pre>
  
  <p>But the script seems to bomb out. I tried to capture the error, but it closes despite me putting <strong>PAUSE</strong> in the script.</p>