I have 1.6 million(!) PDF files in a single folder. The files are all named similar to this:
LAST_FIRST_7-24-1936 Diagnostic - Topography 11-18-10_1.pdf
LAST_FIRST_7-24-1936 Glasses RX 6-1-11_3.pdf
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.
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:
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
I don't know what the proper syntax would be for this.
I did find this link Need a script to create folders based on file names, and auto move files I made this batch based on that link
pushd D:\Data\Medinfo PDFs
for %%F in (*.pdf) do (
2>nul md "%%~nF"
>nul move /y "%%~nF*.*" "%%~nF"
)
popd
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.