Automatically moving and sorting recovered files based on file extension

Automatically moving and sorting recovered files based on file extension

Let’s consider a complex folder/files structure:

recup_dir
| recup_dir.1
| recup_dir.2
| recup_dir.3
| recup_dir.4
| recup_dir.5
| recup_dir.6
| recup_dir.7
...

where recup_dir.[1 to infinity] are subfolders of recup_dir.

This folder structure is generated by PhotoRec, used to recover lost files from damaged/formatted/erased drives. The recovery process will generate a vast amount of recup_dir.X folders with a vast amount of recovered files in each of those folders.

I needed to move all the files found in those (very numerous 1 to infinity) subfolders into different folders based on the file extensions (so in each folder I would only have one filetype, making it a lot easer to browse through and extract important files out of the piles and piles of bulk information).

This would have been a very repetitive time consuming task to perform manually. Thankfully, Windows supports quite a bit of scripting in its Command Interpreter (CMD).

First step is to move all the files from each recup_dir.X subfolder to the parent recup_dir folder (so we can sort them later). Running

for /r %i in (*) do @move "%i" .

via CMD within the recup_dir will automatically perform this action.

 

Moving on, I needed to sort that huge amount of files (over one million at this particular task) to separate (automatically generated) subfolders based on their extensions.

For this task, I created a sort.bat file outside the recup_dir folder (so it doesn’t auto-sort itself) with the following content:

@echo off
cd recup_dir
for %%i in (*.*) do (
echo Attempting to create folder '%%~xi'...
mkdir %%~xi
echo Moving file %%i to %%~xi\%%i
move %%i %%~xi\%%i
)
echo Done.
cd ..

Calling sort.bat via CMD from recup_dir‘s parent folder will process, sort and move all the files to their (automatically created) subfolders based on their extensions.

2 Comments

Leave a Reply