batch-filefile-organization

Bat to sort files by nth character or word into folders named "n"?


I'm trying to make a .bat to sort a large amount of files labeled "(date) (client) - (document Title).pdf" eg "12-21-11 Thompson - Case Management.pdf" into folders titled by client. So far I've gotten:

for /f "tokens=2* delims= " %%b in ('dir /b *.pdf') do (
    md %%b

so that %%b is the client name eg "Thompson" and its respective folder is made, but I'm not sure what to do with move to make it only consider %%b. The titles after "-" aren't consistent for each file, so something like move "%%a %%b %%c %%d" doesn't always work. Is there any way to do this?

Or, if it's easier, is there a way to only consider the 10th character when sorting files? So "12-21-11 Thompson - Case Management" could be sorted into a folder named "T"


Solution

  • Not tested:

     for /f "tokens=2* delims= " %%b in ('dir /b *.pdf') do (
            md %%b > nul 2>&1
            for /f %%F in ('dir /b /a:-d *%%b*.pdf') do (
               move %%F %%b
            )
    
        )