[SCRIPT] Tidy up a folder

Discuss and share scripts and script files...
Post Reply
sj515064
Posts: 51
Joined: 21 Feb 2019 06:14

[SCRIPT] Tidy up a folder

Post by sj515064 »

There may be some folders full of all kinds of stuff on your PC, like the "Desktop" or the "Downloads".

The script below moves files to corresponding folders based on their extensions:
-------------------------------------------------------------------------------------------
Compressed
zip rar r0* r1* arj gz sit sitx sea ace bz2 7z

Documents
doc pdf ppt pps docx pptx

Music
mp3 wav wma mpa ram ra aac aif m4a

Programs
exe msi

Video
avi mpg mpe mpeg asf wmv mov qt rm mp4 flv m4v webm ogv ogg mkv
-------------------------------------------------------------------------------------------

What it really does:
1. Create five subfolders named "Documents", "Programs", "Compressed", "Music", and "Video" in the current folder.
2. Move the files in the current folder to one of the five subfolders above.

Notes:
1. The way of categorizing files is from the Internet Download Manager (IDM).
2. To avoid accidental operation, the path to be tidied up should be explicitly declared at the beginning of the script, one line per path.
For example, "C:\Users\UserName\Downloads".
3. The tags will also be moved (if there is any).
4. If there are already subfolders named "Documents", "Programs", etc. in the current folder then they will be used directly.
5. The way of categorization as well as the types of files to be categorized can be customized by modifying the script.

Screenshot (created with ScreenToGif):
Tidy-up2.gif
Tidy-up2.gif (217.76 KiB) Viewed 1892 times

Code: Select all

$AllowedPaths = <<<PathsAllowedToBeTidiedUp
//Replace this line with the paths you would like to tidy up (one line per path)
PathsAllowedToBeTidiedUp;

 $tk_index = gettokenindex("<curpath>", $AllowedPaths, "<crlf>", "c");
 
 if($tk_index)
 {
 $CurrentContents = listfolder(2:="1", 3:="<crlf>");
 if (!quicksearch("Documents /nd", 3:="n"))
 {new("Documents", "dir");}
 if (!quicksearch("Programs /nd", 3:="n"))
 {new("Programs", "dir");}
 if (!quicksearch("Compressed /nd", 3:="n"))
 {new("Compressed", "dir");}
 if (!quicksearch("Music /nd", 3:="n"))
 {new("Music", "dir");}
 if (!quicksearch("Video /nd", 3:="n"))
 {new("Video", "dir");}

 $Documents = regexmatches($CurrentContents, "^.*\.(doc|docx|pdf|ppt|pps|pptx)$");
 $Programs = regexmatches($CurrentContents, "^.*\.(exe|msi)$");
 $Compressed = regexmatches($CurrentContents, "^.*\.(zip|rar|r\d{1,2}|arj|gz|sit|sitx|sea|ace|bz2|7z)$");
 $Music = regexmatches($CurrentContents, "^.*\.(mp3|wav|wma|mpa|ram|ra|aac|aif|m4a)$");
 $Video = regexmatches($CurrentContents, "^.*\.(avi|mpg|mpe|mpeg|asf|wmv|mov|qt|rm|mp4|flv|m4v|webm|ogv|ogg|mkv)$");

 if($Documents) {moveto("<curpath>\Documents", "$Documents" ,5:="-1");}
 if($Programs) {moveto("<curpath>\Programs", "$Programs" ,5:="-1");}
 if($Compressed) {moveto("<curpath>\Compressed", "$Compressed" ,5:="-1");}
 if($Music) {moveto("<curpath>\Music", "$Music" ,5:="-1");}
 if($Video) {moveto("<curpath>\Video", "$Video" ,5:="-1");}
 
 status "Completed" , 2:="ready"
 }
 else
 {status "Current path is not allowed to be tidied up", "8B4513", "stop"; end true;}
Last edited by sj515064 on 16 Mar 2019 15:47, edited 1 time in total.

highend
Posts: 13274
Joined: 06 Feb 2011 00:33

Re: [SCRIPT] Tidy up a folder

Post by highend »

Easier to setup and extend:

Code: Select all

    $allowedPaths = <<<>>>
// Add all paths that are allowed to be cleaned up (one line per path)
>>>;

    // Add a folder name and its belonging extensions separated with a "="
    $config = <<<>>>
        Documents=doc|docx|pdf|ppt|pps|pptx
        Programs=exe|msi
        Compressed=zip|rar|r\d{1,2}|arj|gz|sit|sitx|sea|ace|bz2|7z
        Music=mp3|wav|wma|mpa|ram|ra|aac|aif|m4a
        Video=avi|mpg|mpe|mpeg|asf|wmv|mov|qt|rm|mp4|flv|m4v|webm|ogv|ogg|mkv
>>>;
    if (strpos($allowedPaths, <curpath>) == -1) {
        status "Current path is NOT in the list of allowed paths, aborted!", "8B4513", "stop"; end true;
    }
    $files = listfolder(2:=1, 3:=<crlf>);
    if !($files) { status "No file(s) in the current folder, aborted!", "8B4513", "stop"; end true; }

    foreach($item, $config, <crlf>, "e") {
        $folderName = trim(gettoken($item, 1, "="));
        $extensions = trim(gettoken($item, 2, "="));
        if (!$folderName || !$extensions) { continue; }

        $matches = regexmatches($files, "^.*\.($extensions)$");
        if ($matches) {
            if (exists("<curpath>\$folderName") != 2) { new($folderName, "dir"); }
            moveto("<curpath>\$folderName", $matches);
        }
    }
    status "Completed";
One of my scripts helped you out? Please donate via Paypal

sj515064
Posts: 51
Joined: 21 Feb 2019 06:14

Re: [SCRIPT] Tidy up a folder

Post by sj515064 »

Great improvement :tup: Thanks!

:)

klownboy
Posts: 4109
Joined: 28 Feb 2012 19:27

Re: [SCRIPT] Tidy up a folder

Post by klownboy »

Nice one guys. It got me thinking that I should really do something with my downloads folder - what a mess. It could stand a bit of organizing. :tup:
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

Post Reply