Skip to content

Instantly share code, notes, and snippets.

@michlbro
Last active February 25, 2023 17:04
Show Gist options
  • Save michlbro/db68d566512a3b39e59f7835f9bc47de to your computer and use it in GitHub Desktop.
Save michlbro/db68d566512a3b39e59f7835f9bc47de to your computer and use it in GitHub Desktop.
File sorter made in python.
import os, shutil
# "filePathType": r"/filepath"
filePaths = {
"video": r"/videos",
"picture": r"/pictures",
"sound": r"/sounds",
"document": r"/documents",
"jar": r"/jars",
"apk": r"/apks",
"pdf": r"/pdfs",
"zip": r"/zips",
"torrent": r"/torrents",
"test": r"/test",
"executable": r"/executables",
"misc": r"/miscs",
}
# "filePathType": [".format", ...]
fileTypes = {
"video": ['.mp4', '.webm', '.mkv', '.mov',],
"picture": ['.png', '.jpeg', '.jpg'],
"document": ['.doc', '.docx', ".txt"],
"pdf": ['.pdf'],
"jar": ['.jar'],
"sound": ['.mp3', '.ogg', '.wav'],
"executable": ['.msi', '.exe'],
"torrent": ['.torrent'],
"zip": ['.zip'],
"rar": ['.RAR'],
"apk": ['.apk']
}
# mainPath is the path where the filePaths exists
# e.g r'C:\Users\ME\Desktop\Sorted Downloads'
mainPath = r'<Path to sorted files>'
# queryPath is a folder where files that need sorting.
# e.g r'C:\Users\ME\Downloads'
queryPath = r'<Path to files that need sorting>'
sortList = os.listdir(queryPath)
for file in sortList:
root, extention = os.path.splitext(file)
foundFileType = None
# Get file format
for fileType in fileTypes:
for format in fileTypes[fileType]:
if extention == format:
foundFileType = fileType
break
if foundFileType:
break
if dict.get(filePaths, foundFileType or "misc") is None:
continue
if not os.path.exists(mainPath + filePaths[foundFileType or "misc"]):
os.makedirs(mainPath + filePaths[foundFileType or "misc"])
if os.path.exists(mainPath + filePaths[foundFileType or "misc"] + "/" + file):
i = 0
while os.path.exists(mainPath + filePaths[foundFileType or "misc"] + "/" + root + "_" + str(i) + extention):
i += 1
os.rename(queryPath + "/" + file, mainPath + filePaths[foundFileType or "misc"] + "/" + root + "_" + str(i) + extention)
continue
shutil.move(queryPath + "/" + file, mainPath + filePaths[foundFileType or "misc"])
@michlbro
Copy link
Author

michlbro commented Feb 25, 2023

You'll need to reconfigure the paths (mainPath, queryPath) it will create the rest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment