0

I am bit of a batch file noob, so I'd really appreciate your help. I have a ton of files stored in a single directory which I wish to sort into subfolders based on the first word in their filenames. So, I have files like these:

C:\Folder\ABC-001
C:\Folder\ABC-002
C:\Folder\EFG-001
C:\Folder\HIJ-002

And I wish to move all files to subfolders based on the first word, ending up like this:

C:\Folder\ABC\ABC-001
C:\Folder\ABC\ABC-002
C:\Folder\EFG\EFG-001
C:\Folder\HIJ\HIJ-002

Please help me get out here. Thank a lot !

harrymc
  • 480,290

1 Answers1

0

The needed adjustments to the proposed duplicate are minimal:

  • Use one for to iterate folder content %%A with a pattern containing at least one dash.
  • Another for /fis needed to split the name %%A at the dash
  • If a folder with the name of %%B doesn't exist create it
  • Move the original file %%A to the subfolder %%B

@Echo off
PushD C:\folder
for %%A in ("*-*.*") do for /f "tokens=1* delims=-" %%B in ("%%A") do (
     If not exist "%%B" MD "%%B"
     Move "%%A" "%%B\"
)
PopD
LotPings
  • 7,231