33import shutil
44
55
6-
7-
86def fix_recursive (library_path ):
97 dupe_folders = []
108 for (root , dirs , files ) in os .walk (library_path , topdown = True ):
@@ -26,41 +24,68 @@ def fix_recursive(library_path):
2624 for d in dirs2 :
2725 full_dir_path = os .path .join (root2 ,d )
2826 # Mark dupe folders with the same name as the current ("parent") folder
29- if os .path .basename (os .path .normpath (d )) == dupe_folder_name and not any (x == full_dir_path for x in dupe_folders ):
30- dupe_folders .append (full_dir_path )
27+ if os .path .basename (os .path .normpath (d )) == dupe_folder_name :
28+ # dupe_folders.append(full_dir_path)
29+
30+ if os .path .exists (full_dir_path ):
31+ # Check before the copy if there's a dupe within a dupe
32+ dupe_folder_children = os .listdir (full_dir_path )
33+ nest_within_nest = dupe_folder_name in dupe_folder_children
3134
35+ log_to_ui (f"Moving { full_dir_path } => { root } " )
36+ try :
37+ shutil .copytree (full_dir_path , root , dirs_exist_ok = True )
38+ except Exception as e :
39+ log_to_ui (f"ERROR MOVING: { e } " )
40+
41+ # Only remove the directory if it's...a directory & empty
42+ if os .path .isdir (full_dir_path ):
43+ while nest_within_nest :
44+ full_dir_path = os .path .join (full_dir_path , dupe_folder_name )
45+ shutil .copytree (full_dir_path , root , dirs_exist_ok = True )
46+ dupe_folder_children = os .listdir (full_dir_path )
47+ nest_within_nest = dupe_folder_name in dupe_folder_children
48+
49+ log_to_ui (f"Deleting copied folder: { full_dir_path } " )
50+ shutil .rmtree (full_dir_path )
51+ else :
52+ log_to_ui (f"Not a folder: { full_dir_path } , skipping........" )
53+ return False
54+
55+ else :
56+ log_to_ui (f"Can't move to { root } " )
57+ log_to_ui (f"Folder doesn't exist (already moved/deleted): { full_dir_path } " )
58+
59+ return False
3260 # Move CONTENTS of the dupes to the top folder, since we don't want to keep the folder itself.
3361 # If dupes were found, break the loop at this level of the directory tree, and start over.abs
34- if len (dupe_folders ) > 0 :
35- # Check for case match
36- # Merge
37- for f in dupe_folders :
38- print (f )
39- if os .path .exists (f ):
40- log_to_ui (f"Moving { f } => { root } " )
41- print (f"Moving { f } => { root } " )
42- shutil .copytree (f , root , dirs_exist_ok = True )
43-
44- # Only remove the directory if it's...a directory & empty
45- if os .path .isdir (f ):
46- if not any (os .scandir (f )):
47- log_to_ui (f"Deleting empty folder: { f } " )
48- shutil .rmtree (f )
49- else :
50- print (any (os .scandir (f )))
51- print (f"Folder not empty, not deleting: { f } " )
52- else :
53- log_to_ui (f"Can't move to { root } " )
54- log_to_ui (f"Folder doesn't exist (already moved/deleted): { f } " )
55- return False
62+ # if len(dupe_folders) > 0:
63+ # # Check for case match
64+ # # Merge
65+ # for f in dupe_folders:
66+ # if os.path.exists(f):
67+ # log_to_ui(f"Moving {f} => {root}")
68+ # try:
69+ # shutil.copytree(f, root, dirs_exist_ok=True)
70+ # # copy_tree(f, root)
71+ # except Exception as e:
72+ # log_to_ui(f"ERROR MOVING: {e}")
73+ # # Only remove the directory if it's...a directory & empty
74+ # if os.path.isdir(f):
75+ # log_to_ui(f"Deleting empty folder: {f}")
76+ # shutil.rmtree(f)
77+ # else:
78+ # log_to_ui(f"Can't move to {root}")
79+ # log_to_ui(f"Folder doesn't exist (already moved/deleted): {f}")
80+ # return False
5681 return True
5782
5883
5984def check (library_path ):
6085 complete = False
6186 counter = 1
6287 while not complete :
63- log_to_ui (f"Checking { library_path } for recursive folders, iteration { counter } " )
88+ log_to_ui (f"Checking { library_path } for recursive folders, iteration { counter } ------------------------------------------------------- " )
6489 complete = fix_recursive (library_path )
6590 counter += 1
6691
0 commit comments