-
Notifications
You must be signed in to change notification settings - Fork 3
/
f2f.py
128 lines (121 loc) · 4.51 KB
/
f2f.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Author: Andrea Mele
# E-mail: [email protected]
# Websites:
# http://www.github.com/AndreaMele
# http://www.artstation.com/AndreaMele
# Project: Files 2 Folders
"""
My scipt for dealing with Radarr Bulk Import
Takes all files, creates directories out of filenames.
Meant to be used after filebot or other pre-organized media files.
Handles duplicate filenames and clean up.
"""
import sys
import os
from pathlib import Path
import os.path
import shutil
def f2f():
os.chdir(sys.path[0])
filecounter = 0
tempDir = os.path.join(sys.path[0] + "\\temp")
tempDirList = []
mainFileList = []
# --- Preventing a mess.
if os.path.exists(tempDir):
print("Temp Dir already exists, cannot proceed")
print("Exiting")
sys.exit()
# --- Tree Log File
os.system("tree /a /f > Logs.txt")
# ----- Dealing with Temp Directory
print("____________")
if not os.path.exists(tempDir):
print("... Temp Dir Created ...")
os.mkdir(tempDir)
else:
for fn in os.listdir(tempDir):
tempDirList.append(fn)
# ----- Dealing with Temp Directory
# --------------------------------------
# ----- File dealings
for root, dirs, files in os.walk(cDir, topdown=True):
# exclude = tempDir
exclude = tempDir
dirs[:] = [d for d in dirs if d not in exclude]
# if root == tempDir:
# print("ding")
# continue
# --------- for file in files
for filename in files:
filecounter += 1
# Separate base from extension
base, extension = os.path.splitext(filename)
# old default File
oldFile = os.path.join(root, filename)
oldFileFolder = os.path.join(root) + "\\"
# New name for Temp Folder
newFile = os.path.join(tempDir, base, filename)
print(" - - Processing File: - - ")
print("oldFile =>", oldFile)
if filename == os.path.basename(sys.argv[0]): #Skip Script File
print("Script file detected. Skipping...")
continue
# If folder basedir/base does not exist... You don't want to create it?
if not os.path.exists(os.path.join(tempDir, base + "\\")):
try:
print("not found ... ", os.path.join(tempDir, base))
os.mkdir(os.path.join(tempDir, base))
print("Folder Created ... ", tempDir, base)
shutil.move(oldFile, newFile)
print("Relocated ... ", newFile)
continue # Next filename
except:
print("Error in try")
# folder exists, file does not
elif not os.path.exists(newFile):
try:
print("Relocated ... ", newFile)
shutil.move(oldFile, newFile)
except:
print("Error in elif")
# folder exists, file exists as well
else:
vNum = 2
while True:
newFile = os.path.join(tempDir, base, base + " - Ver " + str(vNum) + extension)
if not os.path.exists(newFile):
print(f"File : {filename} \nIN {tempDir} ... already exists")
try:
print(f"Renaming : {filename}\n To : {newFile}")
shutil.move(oldFile, newFile)
print(f"Copied : {oldFile}\nTo : {newFile}")
break
except:
print("Error in Else")
vNum += 1
# Clean up old paths
print("_____ CLEAN UP ____")
for root, dirs, files in os.walk(cDir, topdown=True):
exclude = tempDir
dirs[:] = [d for d in dirs if d not in exclude]
for dir in dirs:
shutil.rmtree(dir)
print("Removed : ", dir)
# Moving files back to main directory
tempDirList = os.listdir(tempDir)
for tempFolderName in tempDirList:
tempFolder = os.path.join(tempDir, tempFolderName)
shutil.move(tempFolder, cDir)
# Removing empty temp Dir
shutil.rmtree(tempDir)
print("Removed : ", tempDir)
# --- Tree Log File
os.rename(r"Logs\Logs.txt", r"Logs\Before.txt")
os.system(r"tree /a /f > .\Logs\After.txt")
print("\n\n\t\tall done.")
def main():
f2f()
if __name__ == "__main__":
cDir = os.getcwd()
main()