How to Sort and Move Files Based on their Extensions on Windows

Sometimes your folders could get so cluttered with all sorts of file types in the world. One major example is the Downloads folder where you download and download and never care to organize. Would it be nice if you get to double click somewhere and then automatically all your messy files will be neatly sorted and moved onto their respective folders based on their respective extensions? Well, get ready to embrace the nice then! We are here to guide you on how you can accomplish this very trick with the help of a simple batch script.

Step 1: Navigate to the parent folder which has all the files that need to be sorted and moved. This can be any folder of your choice, including the Downloads folder.

In the example below, in my parent folder, I have many files with various extensions.

 

1 Folder To Sort Min



 

Step 2: As next, right click on an empty space, click on the New option and then click on the Text Document option.

 

2 New Text Min

 

Step 3: You now need to double click on the new text document that you just created. This will open up the newly created text document in your Notepad for editing.

 

3 Open Text Min

 

Step 4: When Notepad launches open, copy and paste the following script onto it.

@echo off

rem thegeekpage.com

for %%a in (".\*") do (
      if "%%~xa" NEQ "" if "%%~dpxa" NEQ "%~dpx0" (
            if not exist "%%~xa" mkdir "%%~xa" (
                   move "%%a" "%%~dpa%%~xa\"
            )
       )
)

 

4 Batch Script Min

 

Once you have copied the above script, press the keys CTRL + S together to save the file. You can close the file once it is saved.

Explanation:

  1. The script iterates through all the files in your parent folder. The batch script we created is ignored and it is not considered for sorting and moving.
  2. The Script takes each file and checks its extension.
  3. The script then checks whether a folder with the same name as the extension of the current file is present in the parent folder or not. If there is no folder, it is created.
  4. Once the folder is created or found to be existing, then the file is moved into that folder.
  5. Steps 2 to 5 are repeated till all the files are checked and moved if required.

Step 5: After that, come back to the parent folder, click on the saved text file and press the key F2 to rename it. You can give any name as per your choice, but make sure you give the extension of the file as bat, after the . (dot). Click somewhere else once you are done with the naming.

Note: Giving the extension as bat is the most important part.

 

5 Rename Min

 

Step 6: When you click somewhere else, you will get the following Rename confirmation window. Click on the Yes button to proceed.

 

6 Confirm Rename Min

 

Step 7: As next, you can double click on the batch file to execute it. Be careful with this step, it cannot be reverted.

 



7 Execute Bat Min

 

Step 8: Viola! You can now see that all the files in your parent folder are now neatly sorted and moved into their respective folders based on their extensions. Enjoy!

 

8 Sorted Min

 

Please tell us in the comments section whether you found the article useful.