title: Batch create folders based on part of file name and move files into that folder
body: <p>I have 1.6 million(!) PDF files in a single folder. The files are all named similar to this:</p>
  
  <pre><code>LAST_FIRST_7-24-1936 Diagnostic - Topography 11-18-10_1.pdf
  LAST_FIRST_7-24-1936 Glasses RX 6-1-11_3.pdf
  </code></pre>
  
  <p>I need to create a folder based on the first part of the file and then move that file and all other files with that same first part of the file name into that folder. In this case the folder would be named "LAST_FIRST_7-24-1936". The folder will always be named the same as the first part of the file up until the space.</p>
  
  <p>I would like to create batch file that will do this. With my rudimentary programming knowledge I came up with this logical process for doing this:</p>
  
  <pre><code>1 Take the first file and name it var1
  2 Remove everything after the space in var1 and name it var2
  3 Create a folder named var2
  4 Move the file var1 into the folder var2
  5 If there are more files Go to line 1, otherwise end
  </code></pre>
  
  <p>I don't know what the proper syntax would be for this.</p>
  
  <p>I did find this link <a href="https://stackoverflow.com/questions/11441317/need-a-script-to-create-folders-based-on-file-names-and-auto-move-files">Need a script to create folders based on file names, and auto move files</a>
  I made this batch based on that link</p>
  
  <pre><code>pushd D:\Data\Medinfo PDFs
  for %%F in (*.pdf) do (
    2&gt;nul md "%%~nF"
    &gt;nul move /y "%%~nF*.*" "%%~nF"
  )
  popd
  </code></pre>
  
  <p>But, it doesn't allow me to create the folder name from just part of the file name. If I can figure that part out I think it would work.
  I know I need to create variable for the folder name but I don't know how to edit the file name variable to remove everything after the space.
  Any help would be appreciated. I'm not opposed to doing this in PowerShell or something else as long as it works natively in Windows Server 2008 R2.</p>