Need a script to sort a load of files into directories

Associate
Joined
3 Oct 2008
Posts
1,890
Location
South London
Calling all scripting gurus, this is probably one small step for you but one giant leap for me.

I have a directory with a tonne of assorted files in there which I want to sort into subdirectories based on the filename.

I.E I'll create a subdir called "A" and I want the script to grab all the files beginning with 'a' and move them into the directory "A".
Rinse and repeat for B, C, D etc.

Ideally a batch script as I could probably handle modifying that to cater for all the new directories I create. VBS or similar I'd need a breakdown of how it works to know what to tweak and what to let be.

Thanks in advance!
 
Code:
C:\Temp>dir /s
 Volume in drive C is Greyhound
 Volume Serial Number is B8BE-3806

 Directory of C:\Temp

11/12/2011  23:40    <DIR>          .
11/12/2011  23:40    <DIR>          ..
11/12/2011  23:40    <DIR>          A
11/12/2011  23:37                 7 afile.txt
11/12/2011  23:37                 7 afile2.txt
11/12/2011  23:40    <DIR>          B
11/12/2011  23:37                 7 bfile.txt
11/12/2011  23:36    <DIR>          C
11/12/2011  23:37                 7 dfile.txt
               4 File(s)             28 bytes

 Directory of C:\Temp\A

11/12/2011  23:40    <DIR>          .
11/12/2011  23:40    <DIR>          ..
               0 File(s)              0 bytes

 Directory of C:\Temp\B

11/12/2011  23:40    <DIR>          .
11/12/2011  23:40    <DIR>          ..
               0 File(s)              0 bytes

 Directory of C:\Temp\C

11/12/2011  23:36    <DIR>          .
11/12/2011  23:36    <DIR>          ..
               0 File(s)              0 bytes

     Total Files Listed:
               4 File(s)             28 bytes
              11 Dir(s)  28,104,212,480 bytes free

C:\Temp>for /D %i in (*) do @move %i* %i 1> nul: 2> nul:

C:\Temp>dir /s
 Volume in drive C is Greyhound
 Volume Serial Number is B8BE-3806

 Directory of C:\Temp

11/12/2011  23:40    <DIR>          .
11/12/2011  23:40    <DIR>          ..
11/12/2011  23:40    <DIR>          A
11/12/2011  23:40    <DIR>          B
11/12/2011  23:36    <DIR>          C
11/12/2011  23:37                 7 dfile.txt
               1 File(s)              7 bytes

 Directory of C:\Temp\A

11/12/2011  23:40    <DIR>          .
11/12/2011  23:40    <DIR>          ..
11/12/2011  23:37                 7 afile.txt
11/12/2011  23:37                 7 afile2.txt
               2 File(s)             14 bytes

 Directory of C:\Temp\B

11/12/2011  23:40    <DIR>          .
11/12/2011  23:40    <DIR>          ..
11/12/2011  23:37                 7 bfile.txt
               1 File(s)              7 bytes

 Directory of C:\Temp\C

11/12/2011  23:36    <DIR>          .
11/12/2011  23:36    <DIR>          ..
               0 File(s)              0 bytes

     Total Files Listed:
               4 File(s)             28 bytes
              11 Dir(s)  28,104,212,480 bytes free

C:\Temp>for /D %i in (*) do @move %i\* . 1> nul: 2> nul:

C:\Temp>dir /s
 Volume in drive C is Greyhound
 Volume Serial Number is B8BE-3806

 Directory of C:\Temp

11/12/2011  23:41    <DIR>          .
11/12/2011  23:41    <DIR>          ..
11/12/2011  23:41    <DIR>          A
11/12/2011  23:37                 7 afile.txt
11/12/2011  23:37                 7 afile2.txt
11/12/2011  23:41    <DIR>          B
11/12/2011  23:37                 7 bfile.txt
11/12/2011  23:36    <DIR>          C
11/12/2011  23:37                 7 dfile.txt
               4 File(s)             28 bytes

 Directory of C:\Temp\A

11/12/2011  23:41    <DIR>          .
11/12/2011  23:41    <DIR>          ..
               0 File(s)              0 bytes

 Directory of C:\Temp\B

11/12/2011  23:41    <DIR>          .
11/12/2011  23:41    <DIR>          ..
               0 File(s)              0 bytes

 Directory of C:\Temp\C

11/12/2011  23:36    <DIR>          .
11/12/2011  23:36    <DIR>          ..
               0 File(s)              0 bytes

     Total Files Listed:
               4 File(s)             28 bytes
              11 Dir(s)  28,104,212,480 bytes free

C:\Temp>

A little tweaking of the parameters in the DO command will allow it to handle subdirectories that aren't in the same location as the files. For debugging purposes I suggest creating a quick set of mock files and directories like I did, remove the @ to see each of the commands the DO is executing, remove the 1> nul: and 2> nul: if you want to see the output (stdout and stderr) of those commands.

Note this is a lengthy read in the code block but most of it is just directory listings, the commands are minimal and it's just all here for demonstration to see the end result of each command. I have also used a revised version of the command to go into each subdirectory and pull the files back out to the main one again as they were to begin with.

When using the FOR ... DO ... command in batch files, you may need to use %%i in some places (I forget offhand where exactly) for it to work. You may also need to experiment with files that contain spaces or ( and ) characters to see it behaves properly with those. Some placements of double-quote " marks around the parameters for the files/paths should sort that out.
 
Last edited:
Code:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
CD /D %1 || GOTO :EOF
FOR /F %%A IN ('dir %1 /b /on /a-d') DO (
SET filename=%%A
  REM cut the first letter of the file
SET letter=!filename:~0,1!
IF NOT EXIST !letter! MKDIR !letter!
ECHO %%A into !letter!
MOVE %%A !letter!
)

pump that into a .bat file and drag and drop the directory containing all the files onto it.

*not tested* (should work) use at own risk. have a backup.
 
Back
Top Bottom