1+ from DAZFix .LibraryFix import log_to_ui
2+ import os
3+ import shutil
4+
5+
6+
7+
8+ def fix_recursive (library_path ):
9+ dupe_folders = []
10+ for (root , dirs , files ) in os .walk (library_path , topdown = True ):
11+ for dir in dirs :
12+ full_directory = os .path .join (root ,dir )
13+ # if current_depth > previous_depth and os.path.basename(os.path.normpath(full_directory)) == previous_root_end_folder:
14+ if os .path .basename (os .path .normpath (root )) == os .path .basename (os .path .normpath (full_directory )):
15+ log_to_ui ("WARNING: Folder appears to have been placed inside itself:" )
16+ log_to_ui (f"Current full path: { full_directory } " )
17+
18+ # FIX #
19+ # Note the top-level folder
20+ # top_folder = root
21+ dupe_folder_name = os .path .basename (os .path .normpath (root ))
22+
23+ # Walk from this folder to find out how many matryoshka folders we have
24+ for (root2 , dirs2 , files2 ) in os .walk (root , topdown = True ):
25+ # Iterate over the directories for this level/root
26+ for d in dirs2 :
27+ full_dir_path = os .path .join (root2 ,d )
28+ # 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 )
31+
32+ # Move CONTENTS of the dupes to the top folder, since we don't want to keep the folder itself.
33+ # 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
56+ return True
57+
58+
59+ def check (library_path ):
60+ complete = False
61+ counter = 1
62+ while not complete :
63+ log_to_ui (f"Checking { library_path } for recursive folders, iteration { counter } " )
64+ complete = fix_recursive (library_path )
65+ counter += 1
66+
67+ log_to_ui ("Done fixing recursive folders!" )
68+
0 commit comments