├── README.md ├── example.png └── f2f.py /README.md: -------------------------------------------------------------------------------- 1 | [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/C0C844OIC) 2 | 3 | # Files2Folders 4 | 5 | **Author:** Andrea Mele 6 | 7 | **E-mail:** andme44@gmial.com 8 | 9 | **Websites:** 10 | > http://www.github.com/AndreaMele 11 | 12 | > http://www.artstation.com/AndreaMele 13 | 14 | 15 | *Takes all files (in all sub/directories), creates directories out of filenames. Handles duplicate filenames and clean up.* 16 | 17 | 18 | **Files 2 Folder Features** 19 | - [x] Takes all files and puts them into folders using the files' name. 20 | - [x] Handles same filenames. 21 | - [x] Sort directories. 22 | - [x] Cleans up previous files. 23 | - [x] Logs Changes to files saved in \Logs\ folder. 24 | 25 | ## When to Use 26 | 27 | Lets say you've used filebot on 100's of files to rename the files, but have forgten to name the folders in the process, or your just happy with the file names and want to create folders using the filename, this will do that. 28 | 29 | Great for further organizing assets that need folder separation. For example: JPEG's that will need iterations. 30 | 31 | Many uses, be creative! 32 | 33 | ## How To Use 34 | 35 | Prerequisites: 36 | [Python 3.8+](https://www.python.org/downloads/) 37 | 38 | **Steps:** 39 | 40 | Place script in root directory of any files. If you want to sort files in your movies folder located at **D:\Movies** place the script in Movies folder so it looks like: **D:\Movies\f2f.py** 41 | 42 | Click **Start** > **Run** , Type: 43 | 44 | Cmd.exe 45 | 46 | Navigate to script location, Type: 47 | 48 | cd /d D:\Movies\ 49 | 50 | Start the script, Type: 51 | 52 | f2f.py 53 | 54 | Example of Results below: 55 | 56 | ![Example of Output](https://raw.githubusercontent.com/AndreaMele/Files2Folders/master/example.png) 57 | -------------------------------------------------------------------------------- /example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndreaMele/Files2Folders/760c62df969c6818698e0ad3c423a988c20a3f9c/example.png -------------------------------------------------------------------------------- /f2f.py: -------------------------------------------------------------------------------- 1 | # Author: Andrea Mele 2 | # E-mail: andme44@gmial.com 3 | # Websites: 4 | # http://www.github.com/AndreaMele 5 | # http://www.artstation.com/AndreaMele 6 | # Project: Files 2 Folders 7 | """ 8 | My scipt for dealing with Radarr Bulk Import 9 | 10 | Takes all files, creates directories out of filenames. 11 | Meant to be used after filebot or other pre-organized media files. 12 | 13 | Handles duplicate filenames and clean up. 14 | """ 15 | 16 | import sys 17 | import os 18 | from pathlib import Path 19 | import os.path 20 | import shutil 21 | 22 | 23 | def f2f(): 24 | os.chdir(sys.path[0]) 25 | filecounter = 0 26 | tempDir = os.path.join(sys.path[0] + "\\temp") 27 | tempDirList = [] 28 | mainFileList = [] 29 | # --- Preventing a mess. 30 | if os.path.exists(tempDir): 31 | print("Temp Dir already exists, cannot proceed") 32 | print("Exiting") 33 | sys.exit() 34 | # --- Tree Log File 35 | os.system("tree /a /f > Logs.txt") 36 | # ----- Dealing with Temp Directory 37 | print("____________") 38 | if not os.path.exists(tempDir): 39 | print("... Temp Dir Created ...") 40 | os.mkdir(tempDir) 41 | else: 42 | for fn in os.listdir(tempDir): 43 | tempDirList.append(fn) 44 | # ----- Dealing with Temp Directory 45 | # -------------------------------------- 46 | # ----- File dealings 47 | for root, dirs, files in os.walk(cDir, topdown=True): 48 | # exclude = tempDir 49 | exclude = tempDir 50 | dirs[:] = [d for d in dirs if d not in exclude] 51 | # if root == tempDir: 52 | # print("ding") 53 | # continue 54 | # --------- for file in files 55 | for filename in files: 56 | filecounter += 1 57 | # Separate base from extension 58 | base, extension = os.path.splitext(filename) 59 | # old default File 60 | oldFile = os.path.join(root, filename) 61 | oldFileFolder = os.path.join(root) + "\\" 62 | # New name for Temp Folder 63 | newFile = os.path.join(tempDir, base, filename) 64 | print(" - - Processing File: - - ") 65 | print("oldFile =>", oldFile) 66 | if filename == os.path.basename(sys.argv[0]): #Skip Script File 67 | print("Script file detected. Skipping...") 68 | continue 69 | # If folder basedir/base does not exist... You don't want to create it? 70 | if not os.path.exists(os.path.join(tempDir, base + "\\")): 71 | try: 72 | print("not found ... ", os.path.join(tempDir, base)) 73 | os.mkdir(os.path.join(tempDir, base)) 74 | print("Folder Created ... ", tempDir, base) 75 | shutil.move(oldFile, newFile) 76 | print("Relocated ... ", newFile) 77 | continue # Next filename 78 | except: 79 | print("Error in try") 80 | # folder exists, file does not 81 | elif not os.path.exists(newFile): 82 | try: 83 | print("Relocated ... ", newFile) 84 | shutil.move(oldFile, newFile) 85 | except: 86 | print("Error in elif") 87 | # folder exists, file exists as well 88 | else: 89 | vNum = 2 90 | while True: 91 | newFile = os.path.join(tempDir, base, base + " - Ver " + str(vNum) + extension) 92 | if not os.path.exists(newFile): 93 | print(f"File : {filename} \nIN {tempDir} ... already exists") 94 | try: 95 | print(f"Renaming : {filename}\n To : {newFile}") 96 | shutil.move(oldFile, newFile) 97 | print(f"Copied : {oldFile}\nTo : {newFile}") 98 | break 99 | except: 100 | print("Error in Else") 101 | vNum += 1 102 | # Clean up old paths 103 | print("_____ CLEAN UP ____") 104 | for root, dirs, files in os.walk(cDir, topdown=True): 105 | exclude = tempDir 106 | dirs[:] = [d for d in dirs if d not in exclude] 107 | for dir in dirs: 108 | shutil.rmtree(dir) 109 | print("Removed : ", dir) 110 | # Moving files back to main directory 111 | tempDirList = os.listdir(tempDir) 112 | for tempFolderName in tempDirList: 113 | tempFolder = os.path.join(tempDir, tempFolderName) 114 | shutil.move(tempFolder, cDir) 115 | # Removing empty temp Dir 116 | shutil.rmtree(tempDir) 117 | print("Removed : ", tempDir) 118 | # --- Tree Log File 119 | os.rename(r"Logs\Logs.txt", r"Logs\Before.txt") 120 | os.system(r"tree /a /f > .\Logs\After.txt") 121 | print("\n\n\t\tall done.") 122 | 123 | def main(): 124 | f2f() 125 | 126 | if __name__ == "__main__": 127 | cDir = os.getcwd() 128 | main() 129 | --------------------------------------------------------------------------------