1
0 Comments

I don’t have much experience with batch files, so I would be grateful for your assistance. I have numerous files saved in one folder, and I want to arrange them into subfolders depending on the initial word in their names. For example, my files are similar to these:

C:\Folder\ABC_001
C:\Folder\BCD_001
C:\Folder\CDF_002
C:\Folder\DFE_003

I want to transfer all the files into the corresponding subfolders based on their first word in the filename, resulting in a structure like this:

C:\Folder\001\ABC_001
C:\Folder\001\BCD_001
C:\Folder\002\CDF_002
C:\Folder\003\DFE_003

Here’s the code I wrote, but it’s not functioning properly:

@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
Askify Moderator Edited question April 25, 2023