"""Zip files by extension Sorts the files in a folder into one ZIP per file type: all SVG files go into "svg.zip", all PNG files into "png.zip", and so on. Free script from LinearAndLoom Icons: https://linearandloomicons.netlify.app/scripts/ Provided as is. Renaming and zipping cannot be undone, so try it on a copy of your folder first. """ import os import zipfile from collections import defaultdict from tkinter import Tk, filedialog def zip_by_extension(folder): groups = defaultdict(list) for filename in os.listdir(folder): filepath = os.path.join(folder, filename) if os.path.isfile(filepath): ext = os.path.splitext(filename)[1].lower().lstrip(".") # ZIP files are left out, so archives from an earlier run are never zipped into themselves if ext and ext != "zip": groups[ext].append(filepath) if not groups: print("No files found.") return for ext, files in groups.items(): zip_path = os.path.join(folder, f"{ext}.zip") with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf: for file in files: zf.write(file, arcname=os.path.basename(file)) print(f"Created: {ext}.zip ({len(files)} files)") # Hide Tk window root = Tk() root.withdraw() while True: folder = filedialog.askdirectory(title="Select Folder to Process") if not folder: print("No folder selected.") else: print(f"\nProcessing: {folder}") zip_by_extension(folder) print("Finished.\n") answer = input("Do you have another folder to process? (Y/N): ").strip().lower() if answer not in ("y", "yes"): break print("Done.") input("Press Enter to exit...")