I have a ton of files stored in a single directory which I wish to sort into subfolders based on the first world in their files names.
So, I have thousands of files that are .pdf files that all have different first words (7 numeric characters).
I also have thousands of folders that have different first words 7 numeric characters in the same directory.
I am trying to move all the .pdfs into folders that have the same first word (7 characters).
So I need a batch file that essentially checks a pdf file and checks in the same directory to see if a folder already exists with the same first 7 numeric characters and if it does moves the .pdf into that folder.
If a folder does not exist for the .pdf, it creates a folder based on the .pdf file's first seven characters.
So for example I have a pdf named "1617832_XXXXXXXX_XXX.pdf" and a folder named "1617832_XXXXXXXX" in the same directory, I want the "1617832_XXXXXXXX_***.pdf" to be moved into the existing folder "1617832_XXXXXXXX". However, if folder ""1617832_XXXXXXXX" does not exist, I want a folder created that is titled "1617832" (the first seven characters of file).
Afterwards doing that, it would move on to the next .pdf "4530834__XXXXXXXX_XXX.pdf" and checks the folders to see if it has a matching folder that has the same first word(seven characters) and then moves the file into that folder (if it exists otherwise creates a new folder).
The only batch that comes close but takes too long to run and doesn't create folders if they don't exist is this one:
@echo on setlocal enabledelayedexpansion cls pushd c:\folders\ for /f "tokens=*" %%1 in ('dir /a-d /b *') do ( set filename=%%1&set dirname=!filename:~0,7! for /f "tokens=*" %%A in ('dir /ad /b') do ( set dirid=%%A&set dirid=!dirid:~0,7! if "!dirid!" equ "!dirname!" move %%1 %%A ) )
Any way to do this more efficiently and effectively?