This script is designed to organize files and folders in your "Downloads" directory by categorizing them into specific subdirectories based on their file types.
Extension from this medium https://blog.mukundmadhav.com/organize-downloads-folder-python-83baf34a05db
- Categorizes files based on their extensions.
- Creates categorized folders if they do not already exist.
- Moves files to their respective categorized folders.
- Moves existing folders to a designated "Folders" directory.
- Ensures folders are not moved into themselves or other categorized folders.
-
Define File Types: The
folder_namesdictionary maps folder names to sets of file extensions. This categorizes various file types by their extensions.folder_names = { "Audio": {'aif','cda','mid','midi','mp3','mpa','ogg','wav','wma','m4a', 'aac'}, "Compressed":{'7z','deb','pkg','rar','rpm', 'tar.gz','z', 'zip'}, 'Code':{'js','jsp','html','ipynb','py','java','css','mhtml','htm','ics','asm', 'circ','csv', 'cms','sql','seb','sh','asc','md','cpp'}, 'Documents':{'ppt','pptx','pdf','xls', 'xlsx','doc','docx','txt', 'tex', 'epub'}, 'Images':{'bmp','gif.ico','jpeg','jpg','png','jfif','svg','tif','tiff','HEIC','JPG','gif','ico','webp'}, 'Softwares':{'apk','bat','bin', 'exe','jar','msi', 'dmg','iso','pyc','stamp'}, 'Videos':{'3gp','avi','flv','h264','mkv','mov','mp4','mpg','mpeg','wmv', 'drp'}, 'Others': {'NONE'} }
-
Extract List of Files and Folders: The script retrieves all files and folders in the "Downloads" directory.
downloads_path = r'C:\Users\Asus\Downloads' onlyfiles = [os.path.join(downloads_path, file) for file in os.listdir(downloads_path) if os.path.isfile(os.path.join(downloads_path, file))] onlyfolders = [os.path.join(downloads_path, file) for file in os.listdir(downloads_path) if not os.path.isfile(os.path.join(downloads_path, file))]
-
Map File Extensions to Folder Names: The
extension_filetype_mapdictionary is created to map each file extension to its corresponding folder name based on thefolder_namesdictionary.extension_filetype_map = {extension: fileType for fileType, extensions in folder_names.items() for extension in extensions }
-
Create Destination Folders: The script generates the full paths for each category folder and creates them if they do not already exist.
folder_paths = [os.path.join(downloads_path, folder_name) for folder_name in folder_names.keys()] folders_directory = os.path.join(downloads_path, 'Folders') [os.mkdir(folderPath) for folderPath in folder_paths if not os.path.exists(folderPath)] if not os.path.exists(folders_directory): os.mkdir(folders_directory)
-
Move Files to Categorized Folders: The
new_pathfunction determines the new path for each file based on its extension. It constructs the new path within the appropriate folder in the "Downloads" directory and moves the file to its new path.def new_path(old_path): extension = str(old_path).split('.')[-1] amplified_folder = extension_filetype_map.get(extension, 'Others') final_path = os.path.join(downloads_path, amplified_folder, str(old_path).split('\\')[-1]) return final_path [Path(eachfile).rename(new_path(eachfile)) for eachfile in onlyfiles]
-
Move Existing Folders to 'Folders' Directory: The script moves each existing folder in the "Downloads" directory to the "Folders" directory, except for predefined folders. If a destination folder already exists, it merges the contents.
for onlyfolder in onlyfolders: folder_name = str(onlyfolder).split('\\')[-1] destination_folder = os.path.join(folders_directory, folder_name) # Skip if the folder is 'Folders' directory itself or in predefined folder names if onlyfolder == folders_directory or folder_name in folder_names.keys(): continue if not os.path.exists(destination_folder): try: Path(onlyfolder).rename(destination_folder) except PermissionError as e: print(f"PermissionError: {e}") else: # If the destination folder already exists, merge the contents try: for sub_file in os.listdir(onlyfolder): Path(os.path.join(onlyfolder, sub_file)).rename(os.path.join(destination_folder, sub_file)) os.rmdir(onlyfolder) except PermissionError as e: print(f"PermissionError: {e}")
To automate the execution of your script using Task Scheduler and run it weekly, follow these steps:
-
Create the Batch Script: Ensure your batch script (
script.bat) contains the following lines and is saved in the specified folder:"C:\Users\Asus\Documents\Vacation Projects\Download Sorter\Download Sorter.py" pause
-
Open Task Scheduler:
- Press
Win + Rto open the Run dialog box. - Type
taskschd.mscand press Enter. This opens the Task Scheduler.
- Press
-
Create a New Task:
- In the Task Scheduler window, click on
Create Taskin the Actions pane on the right.
- In the Task Scheduler window, click on
-
General Settings:
- In the
Generaltab, provide a name for your task (e.g., "Download Sorter Weekly"). - Optionally, add a description.
- Choose
Run whether user is logged on or notif you want the task to run even when you're not logged in. - Check
Run with highest privilegesto ensure the task runs with administrator permissions.
- In the
-
Trigger Settings:
- Go to the
Triggerstab. - Click
Newto create a new trigger. - In the
New Triggerwindow, selectWeekly. - Choose the day(s) of the week you want the task to run.
- Set the start time for the task.
- Click
OKto save the trigger.
- Go to the
-
Action Settings:
- Go to the
Actionstab. - Click
Newto create a new action. - In the
New Actionwindow, set the action toStart a program. - In the
Program/scriptfield, browse to and select your batch script (script.bat). - Click
OKto save the action.
- Go to the
-
Conditions and Settings:
- Optionally, you can configure additional conditions in the
Conditionstab and settings in theSettingstab according to your preferences. - For instance, you might want the task to run only if the computer is idle or to stop the task if it runs longer than a specified duration.
- Optionally, you can configure additional conditions in the
-
Save and Test the Task:
- Click
OKto save the task. - You may be prompted to enter your user password to confirm.
- Once the task is created, you can manually run it by right-clicking on the task in the Task Scheduler library and selecting
Runto ensure it works as expected.
- Click
- Batch Script: Ensure your
script.batfile contains the correct commands. - Task Scheduler: Use Task Scheduler to create a new task that runs weekly, pointing to your
script.batfile.
-
Re-Cluttering Unknown Files: The commented-out sections of the code indicate additional functionality for rechecking and re-categorizing files in the "Others" directory. If you want to enable this functionality, uncomment the relevant sections.
# For Re-Cluttering Unknown files # others_directory = os.path.join(downloads_path, 'Others') # if not os.path.exists(others_directory): # os.mkdir(others_directory) # Recheck files in 'Others' folder and move them to the correct folders # others_files = [os.path.join(others_directory, file) # for file in os.listdir(others_directory) # if os.path.isfile(os.path.join(others_directory, file))] # [Path(eachfile).rename(new_path(eachfile)) for eachfile in others_files]
This script helps maintain an organized "Downloads" directory by ensuring all files and folders are sorted into appropriate categories based on their types.
- Ensure the path to the Python executable is correct.
- Make sure that Task Scheduler has the necessary permissions to run the task.
- If the script requires administrator privileges, make sure to check the
Run with highest privilegesoption.
This setup will automate the execution of your Python script weekly, keeping your Downloads folder organized without manual intervention.