import os
import shutil
import tkinter as tk
from tkinter import filedialog, messagebox, simpledialog
import re
from collections import defaultdict
Initialize global variables
folder_path = ""
groups = {}
display_to_group_map = {}
pending_renames = {} # Stores pending renames: {original_group_name: new_group_name}
pending_merges = [] # Stores pending merges: list of group_names to merge into new_group_name
def clean_filename(filename, redundant_words):
"""Clean filename according to specified rules (remove numbers, repeated words)."""
# 1. Ignore text before and including ':'.
filename = filename.split(':', 1)[-1]
code
# 2. Remove file extension.
filename = os.path.splitext(filename)[0]
# 3. Replace delimiters with spaces, and transform to lowercase.
words = filename.replace('_', ' ').lower().split()
# Check if the entire filename is numeric
if all(part.isdigit() for part in words):
return ' '.join(words)
# 4. Replace numeric groups with spaces
words = [re.sub(r'\d+', ' ', word).strip() for word in words]
# Flatten the words split further on spaces
words = ' '.join(words).split()
# 5. Remove redundant words/groups of characters.
unique_words = [w for w in words if w not in redundant_words]
# 6. Remove words that are less than 3 characters and ensure uniqueness across filename
seen = set()
cleaned_words = [word for word in unique_words if len(word) > 2 and not (word in seen or seen.add(word))]
# Join cleaned words back into a string
cleaned_filename = ' '.join(cleaned_words)
# Use a placeholder for files that end up without significant content
return cleaned_filename if cleaned_filename else "misc_files"
def find_redundant_words(files):
"""Identify groups of characters that appear in all filenames."""
if not files:
print("No files provided to find redundant words.") # Debugging statement
return []
code
Assistant
# Split filenames on ':' and take the relevant part
processed_files = [file.split(':', 1)[-1].replace('_', ' ').lower().split() for file in files]
# Find common words across all processed filenames
common_words = set(processed_files[0])
for file_words in processed_files[1:]:
common_words.intersection_update(file_words)
redundant_words = list(common_words)
print(f"Redundant words identified: {redundant_words}") # Debugging statement
return redundant_words
def extract_sort_key(filename):
"""
Extract numerical suffix from the filename for sorting.
code
Args:
filename (str): The filename.
Returns:
int: Numerical suffix for sorting.
"""
# Extract part after 's_' and before file extension
# Example: 'Ifrete_knit_s_13.jpg' -> 13
match = re.search(r's_(\d+)', filename.lower())
if match:
return int(match.group(1))
return 0 # Default if pattern not found
def prepare_groups(files, grouping_enabled):
"""Prepare groups of files by cleaned filenames or treat all as one group."""
if not files:
print("No files to prepare groups.") # Debugging statement
return {}
code
if grouping_enabled:
# Identify redundant words/groups of characters
redundant_words = find_redundant_words(files)
temp_groups = defaultdict(list)
for file in files:
cleaned_name = clean_filename(file, redundant_words)
print(f"File: {file} => Cleaned Name: {cleaned_name}") # Debugging statement
temp_groups[cleaned_name].append(file)
# Sort files within each group based on numerical suffix
for group in temp_groups:
temp_groups[group].sort(key=extract_sort_key)
print(f"Group '{group}' sorted files: {temp_groups[group]}") # Debugging statement
print(f"Final Groups: {dict(temp_groups)}") # Debugging statement
return temp_groups
else:
# Treat all files as a single group named "All Files"
print("Grouping disabled; treating all files as a single group.") # Debugging statement
return {"All Files": files}
def split_into_subgroups(files, file_num):
"""
Split files into subgroups based on file_num and remaining files rule.
code
Args:
files (list): List of filenames.
file_num (int): Number of files per subgroup.
Returns:
list: List of subgroups (each subgroup is a list of filenames).
"""
subgroups = []
total_files = len(files)
index = 0
while index < total_files:
remaining = total_files - index
# If remaining files are less than twice the file_num and more than file_num, group all remaining
if remaining <= 2 * file_num and remaining > file_num:
subgroups.append(files[index:])
break
else:
subgroups.append(files[index:index + file_num])
index += file_num
print(f"Subgroups created with file_num={file_num}: {subgroups}") # Debugging statement
return subgroups
def capitalize_folder_name(folder_name):
"""Capitalize folder name excluding certain short words."""
short_words = {'of', 'is', 'a', 'an'}
words = folder_name.split()
capitalized = [word.capitalize() if word.lower() not in short_words else word for word in words]
modified_name = ' '.join(capitalized)
print(f"Capitalized folder name: {modified_name}") # Debugging statement
return modified_name
def show_folder_contents(event=None):
"""Display files or subfolders in the selected group (Preview)."""
try:
selection = folder_listbox.curselection()
if not selection:
content_listbox.delete(0, tk.END)
print("No folder selected.") # Debugging statement
return
code
folder_display_name = folder_listbox.get(selection[0])
original_group_name = display_to_group_map.get(folder_display_name, folder_display_name)
print(f"Displaying contents of: {original_group_name}") # Debugging statement
# Clear previous contents
content_listbox.delete(0, tk.END)
# Get the list of files in the selected group, considering pending operations
files_in_group = groups.get(original_group_name, [])
# Apply pending renames or merges for preview
preview_files = files_in_group.copy()
if original_group_name in pending_renames:
new_group_name = pending_renames[original_group_name]
print(f"Preview Rename: '{original_group_name}' will be renamed to '{new_group_name}'") # Debugging
# For merges, the structure is handled separately
if not preview_files:
content_listbox.insert(tk.END, "No files in this group.")
print("No files in this group.") # Debugging statement
return
if group_files_var.get() and enable_grouping_var.get():
file_num_str = file_number_var.get().strip()
if file_num_str:
try:
file_num = int(file_num_str)
if file_num <= 0:
raise ValueError
except ValueError:
messagebox.showerror("Invalid Input", "Please enter a valid positive integer for file grouping.")
content_listbox.delete(0, tk.END) # Clear contents if invalid input
print("Invalid file_num entered.") # Debugging statement
return
# Calculate subgroups based on the file_num
subgroups = split_into_subgroups(preview_files, file_num)
for idx, subgroup in enumerate(subgroups, 1):
# Update subfolder naming to include a hyphen separator
subfolder_display_name = f"{folder_display_name} - {idx}"
content_listbox.insert(tk.END, f"{subfolder_display_name}:")
for file in subgroup:
content_listbox.insert(tk.END, f" - {file}")
else:
# If file_num is empty, just display the grouped files without further subgroups
for file in preview_files:
content_listbox.insert(tk.END, file)
elif group_files_var.get() and not enable_grouping_var.get():
file_num_str = file_number_var.get().strip()
if file_num_str:
try:
file_num = int(file_num_str)
if file_num <= 0:
raise ValueError
except ValueError:
messagebox.showerror("Invalid Input", "Please enter a valid positive integer for file grouping.")
content_listbox.delete(0, tk.END) # Clear contents if invalid input
print("Invalid file_num entered.") # Debugging statement
return
# Calculate subgroups based on the file_num
subgroups = split_into_subgroups(preview_files, file_num)
for idx, subgroup in enumerate(subgroups, 1):
# Use parent folder name as the base for subfolder naming
subfolder_display_name = f"{parent_name_var.get().strip()} - {idx}"
content_listbox.insert(tk.END, f"{subfolder_display_name}:")
for file in subgroup:
content_listbox.insert(tk.END, f" - {file}")
else:
# If file_num is empty, just display all files without subgroups
for file in preview_files:
content_listbox.insert(tk.END, file)
else:
for file in preview_files:
content_listbox.insert(tk.END, file)
except Exception as e:
messagebox.showerror("Error", f"An error occurred while displaying folder contents:\n{str(e)}")
print(f"Error in show_folder_contents: {str(e)}")
def load_directory():
"""Load the selected directory, prepare groups, and update the displayed folders."""
global folder_path, groups, pending_renames, pending_merges
print("Load Directory button clicked.") # Debugging statement
folder_path = filedialog.askdirectory(title="Select the folder containing your files")
print(f"Selected folder: {folder_path}") # Debugging statement
if not folder_path:
print("No folder selected.") # Debugging statement
return
parent_folder_name = os.path.basename(folder_path)
parent_name_var.set(parent_folder_name)
try:
# Only consider files, ignore subdirectories
files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
print(f"Files found: {files}") # Debugging statement
groups = prepare_groups(files, enable_grouping_var.get())
print(f"Groups prepared: {groups}") # Debugging statement
# Reset any pending operations
pending_renames.clear()
pending_merges.clear()
update_displayed_folders()
except Exception as e:
messagebox.showerror("Error", f"An error occurred while loading the directory:\n{str(e)}")
print(f"Error in load_directory: {str(e)}")
def apply_options(*args):
"""Apply the selected options and update the preview."""
if not folder_path:
print("No directory loaded; skipping apply_options.") # Debugging statement
return # Do nothing if no directory is loaded
try:
load_directory_preview()
except Exception as e:
messagebox.showerror("Error", f"An error occurred while applying options:\n{str(e)}")
print(f"Error in apply_options: {str(e)}")
def load_directory_preview():
"""Load the directory and update the display based on the current settings without moving files."""
global groups
try:
# Only consider files, ignore subdirectories
files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
print(f"Preview - Files found: {files}") # Debugging statement
groups = prepare_groups(files, enable_grouping_var.get())
print(f"Preview - Groups prepared: {groups}") # Debugging statement
update_displayed_folders()
except Exception as e:
messagebox.showerror("Error", f"An error occurred while updating the preview:\n{str(e)}")
print(f"Error in load_directory_preview: {str(e)}")
def update_displayed_folders():
"""Update the folder_listbox based on current groups and selected options."""
global display_to_group_map
parent_prefix = parent_name_var.get().strip()
add_prefix = prefix_var.get()
capitalize_names = capitalize_var.get()
code
folder_listbox.delete(0, tk.END)
display_to_group_map.clear()
for folder_name in sorted(groups.keys()):
# Determine the display folder name based on current settings and pending renames
display_name = folder_name
if folder_name in pending_renames:
display_name = pending_renames[folder_name]
print(f"Pending Rename: '{folder_name}' will be displayed as '{display_name}'") # Debugging
if enable_grouping_var.get():
# When grouping is enabled
if folder_name != "All Files":
if capitalize_names:
modified_folder_name = capitalize_folder_name(display_name)
else:
modified_folder_name = display_name
if add_prefix:
if modified_folder_name:
# Prefix is added after capitalization
modified_folder_name = f"{parent_prefix} - {modified_folder_name}"
else:
# If group folder name is empty, only prefix
modified_folder_name = parent_prefix
# else, no prefix added
else:
# When grouping is enabled but all files are under "All Files"
# Treat similarly to grouping disabled
modified_folder_name = parent_prefix
else:
# When grouping is disabled
if add_prefix:
# Prefix is used as the base name for subfolders
modified_folder_name = parent_prefix
else:
# No prefix; since grouping is disabled, "All Files" is treated as the base
modified_folder_name = parent_prefix
folder_listbox.insert(tk.END, modified_folder_name)
display_to_group_map[modified_folder_name] = folder_name
print(f"Displayed folder: {modified_folder_name} mapped to group: {folder_name}") # Debugging statement
# Automatically select the first folder and show its contents
if folder_listbox.size() > 0:
folder_listbox.select_set(0)
show_folder_contents()
else:
print("No groups to display.") # Debugging statement
def execute_sorting():
"""Execute the sorting: move files into organized folders and subfolders based on selected options."""
if not folder_path or not groups:
messagebox.showerror("Error", "No directory loaded or no files grouped. Please load a directory first.")
print("Execute Sorting failed: No directory loaded or no files grouped.") # Debugging statement
return
code
try:
parent_prefix = parent_name_var.get().strip()
add_prefix = prefix_var.get()
group_files = group_files_var.get()
capitalize_names = capitalize_var.get()
grouping_enabled = enable_grouping_var.get()
file_num_str = file_number_var.get().strip()
# Determine file_num only if group_files is enabled and file_num_str is not empty
if group_files and file_num_str:
try:
file_num = int(file_num_str)
if file_num <= 0:
raise ValueError
except ValueError:
messagebox.showerror("Invalid Input", "Please enter a valid positive integer for file grouping.")
print("Invalid file_num entered during execute_sorting.") # Debugging statement
return
else:
file_num = None # No grouping by file number
# Apply pending renames
for original_group, new_group in pending_renames.items():
if original_group not in groups:
continue
# Update the group name
groups[new_group] = groups.pop(original_group)
print(f"Renaming group from '{original_group}' to '{new_group}'") # Debugging statement
# Clear pending renames after applying
pending_renames.clear()
# Apply pending merges
for merge_operation in pending_merges:
group_names = merge_operation['groups']
new_group_name = merge_operation['new_group']
merged_files = []
# Create new group
groups[new_group_name] = merged_files
for group_name in group_names:
if group_name not in groups:
continue
merged_files.extend(groups[group_name])
del groups[group_name]
print(f"Merging group '{group_name}' into '{new_group_name}'") # Debugging statement
# Clear pending merges after applying
pending_merges.clear()
for folder_display_name, group_name in display_to_group_map.items():
files = groups.get(group_name, [])
if grouping_enabled and group_name != "All Files":
# Grouping Enabled and multiple groups
if capitalize_names:
base_folder_name = capitalize_folder_name(group_name)
else:
base_folder_name = group_name
if add_prefix:
if base_folder_name:
# Prefix is added after capitalization
modified_folder_name = f"{parent_prefix} - {base_folder_name}"
else:
# If group folder name is empty, only prefix
modified_folder_name = parent_prefix
else:
modified_folder_name = base_folder_name
new_folder_path = os.path.join(folder_path, modified_folder_name)
# Prevent creating a folder with the same name as the parent folder
if os.path.abspath(new_folder_path) == os.path.abspath(folder_path):
messagebox.showerror("Error", f"Cannot create a folder with the same name as the parent folder: {modified_folder_name}")
print(f"Cannot create folder with the same name as parent: {modified_folder_name}") # Debugging statement
continue
os.makedirs(new_folder_path, exist_ok=True)
print(f"Created folder: {new_folder_path}") # Debugging statement
if group_files and file_num:
subgroups = split_into_subgroups(files, file_num)
for idx, subgroup in enumerate(subgroups, 1):
subfolder_name = f"{modified_folder_name} - {idx}"
subfolder_path = os.path.join(new_folder_path, subfolder_name)
os.makedirs(subfolder_path, exist_ok=True)
print(f"Created subfolder: {subfolder_path}") # Debugging statement
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
print(f"Moved '{file}' to '{subfolder_path}'") # Debugging statement
else:
for file in files:
src = os.path.join(folder_path, file)
dest = os.path.join(new_folder_path, file)
shutil.move(src, dest)
print(f"Moved '{file}' to '{new_folder_path}'") # Debugging statement
elif not grouping_enabled and group_name == "All Files":
# Grouping Disabled: Create subfolders based on file_num directly in the parent directory
if group_files and file_num:
subgroups = split_into_subgroups(files, file_num)
for idx, subgroup in enumerate(subgroups, 1):
subfolder_name = f"{parent_prefix} - {idx}"
subfolder_path = os.path.join(folder_path, subfolder_name)
# Prevent creating a folder with the same name as the parent folder
if os.path.abspath(subfolder_path) == os.path.abspath(folder_path):
messagebox.showerror("Error", f"Cannot create a subfolder with the same name as the parent folder: {subfolder_name}")
print(f"Cannot create subfolder with the same name as parent: {subfolder_name}") # Debugging statement
continue
os.makedirs(subfolder_path, exist_ok=True)
print(f"Created subfolder: {subfolder_path}") # Debugging statement
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
print(f"Moved '{file}' to '{subfolder_path}'") # Debugging statement
elif not group_files:
# If grouping is disabled and file grouping is not enabled,
# and all files are already in the parent folder, do nothing
print("Grouping disabled and file grouping not enabled; no action taken.") # Debugging statement
pass
else:
# Edge Case: grouping_enabled but only "All Files" group exists
# Treat it as grouping disabled
if group_files and file_num:
subgroups = split_into_subgroups(files, file_num)
for idx, subgroup in enumerate(subgroups, 1):
subfolder_name = f"{parent_prefix} - {idx}"
subfolder_path = os.path.join(folder_path, subfolder_name)
# Prevent creating a folder with the same name as the parent folder
if os.path.abspath(subfolder_path) == os.path.abspath(folder_path):
messagebox.showerror("Error", f"Cannot create a subfolder with the same name as the parent folder: {subfolder_name}")
print(f"Cannot create subfolder with the same name as parent: {subfolder_name}") # Debugging statement
continue
os.makedirs(subfolder_path, exist_ok=True)
print(f"Created subfolder: {subfolder_path}") # Debugging statement
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
print(f"Moved '{file}' to '{subfolder_path}'") # Debugging statement
elif not group_files:
# Move all files into a single subfolder named with parent prefix
subfolder_name = f"{parent_prefix}"
subfolder_path = os.path.join(folder_path, subfolder_name)
# Prevent creating a folder with the same name as the parent folder
if os.path.abspath(subfolder_path) == os.path.abspath(folder_path):
# If the subfolder name is the same as the parent folder, skip creation
print(f"Subfolder name same as parent; skipping creation: {subfolder_name}") # Debugging statement
pass
else:
os.makedirs(subfolder_path, exist_ok=True)
print(f"Created subfolder: {subfolder_path}") # Debugging statement
for file in files:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
print(f"Moved '{file}' to '{subfolder_path}'") # Debugging statement
messagebox.showinfo("Success", "Files sorted and organized successfully!")
load_directory() # Reload to reflect changes without opening the folder
print("Files sorted and organized successfully.") # Debugging statement
except Exception as e:
messagebox.showerror("Error", f"An error occurred during sorting:\n{str(e)}")
print(f"Error in execute_sorting: {str(e)}")
---------------------------- New Functions ----------------------------
def rename_selected_folder():
"""Rename the selected folder directly in the listbox."""
selected_indices = folder_listbox.curselection()
if len(selected_indices) != 1:
messagebox.showinfo("Rename Folder", "Please select exactly one folder to rename.")
print("Rename operation aborted: Incorrect number of selections.") # Debugging statement
return
code
index = selected_indices[0]
old_display_name = folder_listbox.get(index)
original_group_name = display_to_group_map.get(old_display_name, old_display_name)
print(f"Renaming folder: {original_group_name}") # Debugging statement
# Create an Entry widget overlaying the listbox item
entry = tk.Entry(frame, width=40)
entry.insert(0, old_display_name)
entry.select_range(0, tk.END)
entry.focus()
# Get the coordinates of the selected item
bbox = folder_listbox.bbox(index)
if bbox:
entry.place(x=folder_listbox.winfo_x() + bbox[0], y=folder_listbox.winfo_y() + bbox[1], width=bbox[2], height=bbox[3])
print(f"Entry widget placed at x={folder_listbox.winfo_x() + bbox[0]}, y={folder_listbox.winfo_y() + bbox[1]}") # Debugging statement
def on_enter(event=None):
new_display_name = entry.get().strip()
print(f"New display name entered: {new_display_name}") # Debugging statement
if not new_display_name:
messagebox.showerror("Invalid Name", "Folder name cannot be empty.")
print("Rename operation aborted: Empty name.") # Debugging statement
return
if new_display_name in display_to_group_map and new_display_name not in pending_renames.values():
messagebox.showerror("Duplicate Name", "A folder with this name already exists.")
print("Rename operation aborted: Duplicate name.") # Debugging statement
return
# Ensure new_display_name does not create conflict in pending renames
for pending_new in pending_renames.values():
if new_display_name == pending_new:
messagebox.showerror("Duplicate Name", "A pending rename to this name already exists.")
print("Rename operation aborted: Pending duplicate name.") # Debugging statement
return
# Register the pending rename
pending_renames[original_group_name] = new_display_name
print(f"Registered pending rename: '{original_group_name}' -> '{new_display_name}'") # Debugging statement
# Update the listbox to reflect the pending rename
folder_listbox.delete(index)
folder_listbox.insert(index, new_display_name)
display_to_group_map[new_display_name] = original_group_name
del display_to_group_map[old_display_name]
print(f"Updated listbox and mapping with pending rename '{new_display_name}'") # Debugging statement
entry.destroy()
# Update the preview to reflect the rename
show_folder_contents()
print(f"Pending rename '{original_group_name}' -> '{new_display_name}' displayed.") # Debugging statement
# Bind Enter key to submit the rename
entry.bind("<Return>", on_enter)
# Bind clicking anywhere else to submit the rename
def on_click_anywhere(event):
widget = event.widget
if widget != entry:
on_enter()
root.bind("<Button-1>", on_click_anywhere)
# Ensure that clicking outside destroys the entry widget
def on_focus_out(event):
on_enter()
entry.bind("<FocusOut>", on_focus_out)
def merge_selected_folders():
"""Merge the selected folders into a single folder."""
selected_indices = folder_listbox.curselection()
if len(selected_indices) < 2:
messagebox.showinfo("Merge Folders", "Please select two or more folders to merge.")
print("Merge operation aborted: Less than two folders selected.") # Debugging statement
return
code
selected_display_names = [folder_listbox.get(i) for i in selected_indices]
selected_group_names = [display_to_group_map.get(name, name) for name in selected_display_names]
print(f"Merging folders: {selected_group_names}") # Debugging statement
# Suggest the first selected folder's name as default
default_new_folder_name = selected_display_names[0]
new_folder_name = simpledialog.askstring("Merge Folders", "Enter the name for the merged folder:", initialvalue=default_new_folder_name)
print(f"New folder name for merge: {new_folder_name}") # Debugging statement
if not new_folder_name:
messagebox.showerror("Invalid Name", "Folder name cannot be empty.")
print("Merge operation aborted: Empty new folder name.") # Debugging statement
return
if new_folder_name in display_to_group_map and new_folder_name not in [merge['new_group'] for merge in pending_merges]:
messagebox.showerror("Duplicate Name", "A folder with this name already exists.")
print("Merge operation aborted: Duplicate new folder name.") # Debugging statement
return
# Ensure new_folder_name does not conflict with existing pending merges
for merge_op in pending_merges:
if new_folder_name == merge_op['new_group']:
messagebox.showerror("Duplicate Name", "A pending merge to this name already exists.")
print("Merge operation aborted: Pending duplicate new folder name.") # Debugging statement
return
# Register the pending merge
pending_merges.append({'groups': selected_group_names, 'new_group': new_folder_name})
print(f"Registered pending merge: {selected_group_names} -> '{new_folder_name}'") # Debugging statement
# Update the listbox to reflect the pending merge
# Remove merged folders from display
for i in reversed(selected_indices):
display_name = folder_listbox.get(i)
folder_listbox.delete(i)
del display_to_group_map[display_name]
print(f"Removed '{display_name}' from display list for merging.") # Debugging statement
# Add the new merged folder to the display list
folder_listbox.insert(tk.END, new_folder_name)
display_to_group_map[new_folder_name] = new_folder_name
print(f"Added merged folder '{new_folder_name}' to display list.") # Debugging statement
# Update the preview to reflect the merge
show_folder_contents()
print(f"Pending merge displayed as '{new_folder_name}'") # Debugging statement
def create_context_menu(event):
"""Create and display a context menu for the folder listbox."""
try:
context_menu = tk.Menu(root, tearoff=0)
context_menu.add_command(label="Rename", command=rename_selected_folder)
code
# Check if multiple selections exist for merging
selected_indices = folder_listbox.curselection()
if len(selected_indices) > 1:
context_menu.add_command(label="Merge Selected Folders", command=merge_selected_folders)
context_menu.post(event.x_root, event.y_root)
except Exception as e:
messagebox.showerror("Error", f"An error occurred while creating the context menu:\n{str(e)}")
print(f"Error creating context menu: {str(e)}") # Debugging statement
When I try to merge the following folders, I use the default merge folder name (Fur Coat).
Fur Coat
Fur Coat Furcoat
Furcoat
it gives me a warning saying "A folder with this name already exists".
If I were to move the content from Fur Coat Furcoat and Furcoat to Fur Coat, it should not give me this error.
More importantly, when I rename the merge folder using the word Fur, the folder will appear in the Folder Listbox but without any content.
Thank you for providing the detailed script and describing the issues you're encountering. Let's address each bug systematically and provide the necessary fixes.
Bug 1: Merging into an Existing Folder Name Causes a Duplicate Name Error
Issue Explanation:
When you attempt to merge folders and use a name that already exists (e.g., "Fur Coat"), the script incorrectly identifies this as a duplicate, even if the target folder is among the folders being merged. This prevents merging folders into an existing group name when it should be allowed.
Cause:
In the merge_selected_folders function, the script checks if the new folder name already exists in display_to_group_map without considering if the name is part of the folders being merged. This leads to a false positive for duplicates when merging into an existing folder.
Solution:
Modify the duplicate name check to allow using a name that is among the selected groups being merged. This ensures that merging into an existing group name doesn't trigger the duplicate name error.
Code Fix:
Update the Duplicate Name Check in merge_selected_folders:
Modify the condition to exclude the groups being merged from the duplicate check.
python
def merge_selected_folders():
"""Merge the selected folders into a single folder."""
selected_indices = folder_listbox.curselection()
if len(selected_indices) < 2:
messagebox.showinfo("Merge Folders", "Please select two or more folders to merge.")
print("Merge operation aborted: Less than two folders selected.") # Debugging statement
return
selected_display_names = [folder_listbox.get(i) for i in selected_indices]
selected_group_names = [display_to_group_map.get(name, name) for name in selected_display_names]
print(f"Merging folders: {selected_group_names}") # Debugging statement
# Suggest the first selected folder's name as default
default_new_folder_name = selected_display_names[0]
new_folder_name = simpledialog.askstring("Merge Folders", "Enter the name for the merged folder:", initialvalue=default_new_folder_name)
print(f"New folder name for merge: {new_folder_name}") # Debugging statement
if not new_folder_name:
messagebox.showerror("Invalid Name", "Folder name cannot be empty.")
print("Merge operation aborted: Empty new folder name.") # Debugging statement
return
# Allow using a name that's among the selected groups being merged
if (new_folder_name in display_to_group_map and
display_to_group_map[new_folder_name] not in selected_group_names and
new_folder_name not in [merge['new_group'] for merge in pending_merges]):
messagebox.showerror("Duplicate Name", "A folder with this name already exists.")
print("Merge operation aborted: Duplicate new folder name.") # Debugging statement
return
# Ensure new_folder_name does not conflict with existing pending merges
for merge_op in pending_merges:
if new_folder_name == merge_op['new_group']:
messagebox.showerror("Duplicate Name", "A pending merge to this name already exists.")
print("Merge operation aborted: Pending duplicate new folder name.") # Debugging statement
return
# Register the pending merge
pending_merges.append({'groups': selected_group_names, 'new_group': new_folder_name})
print(f"Registered pending merge: {selected_group_names} -> '{new_folder_name}'") # Debugging statement
# Update the listbox to reflect the pending merge
# Remove merged folders from display
for i in reversed(selected_indices):
display_name = folder_listbox.get(i)
folder_listbox.delete(i)
del display_to_group_map[display_name]
print(f"Removed '{display_name}' from display list for merging.") # Debugging statement
# Add the new merged folder to the display list
folder_listbox.insert(tk.END, new_folder_name)
display_to_group_map[new_folder_name] = new_folder_name
print(f"Added merged folder '{new_folder_name}' to display list.") # Debugging statement
# Update the preview to reflect the merge
show_folder_contents()
print(f"Pending merge displayed as '{new_folder_name}'") # Debugging statement
Update the Merge Application in execute_sorting:
Modify the merging logic to append files to an existing group if the merged folder name already exists, instead of overwriting it.
python
Bug 2: Renamed Merged Folder Appears Without Content
Issue Explanation:
After merging folders and renaming the merged folder (e.g., renaming to "Fur"), the folder appears in the Folder Listbox but without displaying any of its contents.
Cause:
The issue likely stems from how the script handles the mapping between display names and group names during the renaming process, especially after merging. If the display_to_group_map does not correctly reference the merged group's files after renaming, the contents won't be displayed.
Solution:
Ensure that when a folder is renamed—especially after a merge—the display_to_group_map correctly points to the merged group's name so that show_folder_contents can retrieve and display the appropriate files.
Code Fix:
Ensure Correct Mapping After Renaming:
Update the rename_selected_folder function to correctly maintain the association between the new display name and the merged group name.
python
Final Corrected Code Snippets
Below are the fully corrected functions incorporating the fixes described above:
1. merge_selected_folders Function
python
def merge_selected_folders():
"""Merge the selected folders into a single folder."""
selected_indices = folder_listbox.curselection()
if len(selected_indices) < 2:
messagebox.showinfo("Merge Folders", "Please select two or more folders to merge.")
print("Merge operation aborted: Less than two folders selected.") # Debugging statement
return
selected_display_names = [folder_listbox.get(i) for i in selected_indices]
selected_group_names = [display_to_group_map.get(name, name) for name in selected_display_names]
print(f"Merging folders: {selected_group_names}") # Debugging statement
# Suggest the first selected folder's name as default
default_new_folder_name = selected_display_names[0]
new_folder_name = simpledialog.askstring("Merge Folders", "Enter the name for the merged folder:", initialvalue=default_new_folder_name)
print(f"New folder name for merge: {new_folder_name}") # Debugging statement
if not new_folder_name:
messagebox.showerror("Invalid Name", "Folder name cannot be empty.")
print("Merge operation aborted: Empty new folder name.") # Debugging statement
return
# Allow using a name that's among the selected groups being merged
if (new_folder_name in display_to_group_map and
display_to_group_map[new_folder_name] not in selected_group_names and
new_folder_name not in [merge['new_group'] for merge in pending_merges]):
messagebox.showerror("Duplicate Name", "A folder with this name already exists.")
print("Merge operation aborted: Duplicate new folder name.") # Debugging statement
return
# Ensure new_folder_name does not conflict with existing pending merges
for merge_op in pending_merges:
if new_folder_name == merge_op['new_group']:
messagebox.showerror("Duplicate Name", "A pending merge to this name already exists.")
print("Merge operation aborted: Pending duplicate new folder name.") # Debugging statement
return
# Register the pending merge
pending_merges.append({'groups': selected_group_names, 'new_group': new_folder_name})
print(f"Registered pending merge: {selected_group_names} -> '{new_folder_name}'") # Debugging statement
# Update the listbox to reflect the pending merge
# Remove merged folders from display
for i in reversed(selected_indices):
display_name = folder_listbox.get(i)
folder_listbox.delete(i)
del display_to_group_map[display_name]
print(f"Removed '{display_name}' from display list for merging.") # Debugging statement
# Add the new merged folder to the display list
folder_listbox.insert(tk.END, new_folder_name)
display_to_group_map[new_folder_name] = new_folder_name
print(f"Added merged folder '{new_folder_name}' to display list.") # Debugging statement
# Update the preview to reflect the merge
show_folder_contents()
print(f"Pending merge displayed as '{new_folder_name}'") # Debugging statement
2. execute_sorting Function
python
3. rename_selected_folder Function
python
def rename_selected_folder():
"""Rename the selected folder directly in the listbox."""
selected_indices = folder_listbox.curselection()
if len(selected_indices) != 1:
messagebox.showinfo("Rename Folder", "Please select exactly one folder to rename.")
print("Rename operation aborted: Incorrect number of selections.") # Debugging statement
return
index = selected_indices[0]
old_display_name = folder_listbox.get(index)
original_group_name = display_to_group_map.get(old_display_name, old_display_name)
print(f"Renaming folder: {original_group_name}") # Debugging statement
# Create an Entry widget overlaying the listbox item
entry = tk.Entry(frame, width=40)
entry.insert(0, old_display_name)
entry.select_range(0, tk.END)
entry.focus()
# Get the coordinates of the selected item
bbox = folder_listbox.bbox(index)
if bbox:
entry.place(x=folder_listbox.winfo_x() + bbox[0], y=folder_listbox.winfo_y() + bbox[1], width=bbox[2], height=bbox[3])
print(f"Entry widget placed at x={folder_listbox.winfo_x() + bbox[0]}, y={folder_listbox.winfo_y() + bbox[1]}") # Debugging statement
def on_enter(event=None):
new_display_name = entry.get().strip()
print(f"New display name entered: {new_display_name}") # Debugging statement
if not new_display_name:
messagebox.showerror("Invalid Name", "Folder name cannot be empty.")
print("Rename operation aborted: Empty name.") # Debugging statement
return
if new_display_name in display_to_group_map and new_display_name != old_display_name:
messagebox.showerror("Duplicate Name", "A folder with this name already exists.")
print("Rename operation aborted: Duplicate name.") # Debugging statement
return
# Register the pending rename
pending_renames[original_group_name] = new_display_name
print(f"Registered pending rename: '{original_group_name}' -> '{new_display_name}'") # Debugging statement
# Update the listbox to reflect the pending rename
folder_listbox.delete(index)
folder_listbox.insert(index, new_display_name)
display_to_group_map[new_display_name] = original_group_name
del display_to_group_map[old_display_name]
print(f"Updated listbox and mapping with pending rename '{new_display_name}'") # Debugging statement
entry.destroy()
# Update the preview to reflect the rename
show_folder_contents()
print(f"Pending rename '{original_group_name}' -> '{new_display_name}' displayed.") # Debugging statement
# Bind Enter key to submit the rename
entry.bind("<Return>", on_enter)
# Bind clicking anywhere else to submit the rename
def on_click_anywhere(event):
widget = event.widget
if widget != entry:
on_enter()
root.bind("<Button-1>", on_click_anywhere)
# Ensure that clicking outside destroys the entry widget
def on_focus_out(event):
on_enter()
entry.bind("<FocusOut>", on_focus_out)
Testing the Fixes
After implementing the above changes, perform the following tests to ensure that the bugs are resolved:
Test Bug 1: Merging into an Existing Folder Name
Select multiple folders, including one that you intend to merge into (e.g., "Fur Coat Furcoat" and "Furcoat").
Initiate the merge operation and use an existing folder name (e.g., "Fur Coat") as the merged folder name.
The merge should proceed without raising a duplicate name error, and the contents of the merged folders should be appropriately moved into "Fur Coat".
Test Bug 2: Renamed Merged Folder Displays Content
After merging folders, rename the merged folder to a new name (e.g., "Fur").
Verify that the renamed folder appears in the Folder Listbox and that selecting it displays all the expected contents in the Folder Contents (Preview) listbox.
Additional Tests:
Attempt to rename a folder to an existing non-merged folder name to ensure that the duplicate name error is correctly raised.
Merge multiple folders into a new, unique folder name and verify that all contents are correctly moved and displayed.
Test edge cases, such as renaming to empty strings or invalid characters, to ensure proper error handling.
Conclusion
By adjusting the duplicate name checks in the merge_selected_folders and execute_sorting functions and ensuring that the rename_selected_folder function maintains accurate mappings, both of your described bugs should be resolved. These changes allow for smooth merging into existing folder names and ensure that renamed folders display their contents correctly.
If you encounter any further issues or need additional assistance, feel free to ask!
def execute_sorting():
"""Execute the sorting: move files into organized folders and subfolders based on selected options."""
if not folder_path or not groups:
messagebox.showerror("Error", "No directory loaded or no files grouped. Please load a directory first.")
print("Execute Sorting failed: No directory loaded or no files grouped.") # Debugging statement
return
try:
parent_prefix = parent_name_var.get().strip()
add_prefix = prefix_var.get()
group_files = group_files_var.get()
capitalize_names = capitalize_var.get()
grouping_enabled = enable_grouping_var.get()
file_num_str = file_number_var.get().strip()
# Determine file_num only if group_files is enabled and file_num_str is not empty
if group_files and file_num_str:
try:
file_num = int(file_num_str)
if file_num <= 0:
raise ValueError
except ValueError:
messagebox.showerror("Invalid Input", "Please enter a valid positive integer for file grouping.")
print("Invalid file_num entered during execute_sorting.") # Debugging statement
return
else:
file_num = None # No grouping by file number
# Apply pending renames
for original_group, new_group in pending_renames.items():
if original_group not in groups:
continue
# Update the group name
groups[new_group] = groups.pop(original_group)
print(f"Renaming group from '{original_group}' to '{new_group}'") # Debugging statement
# Clear pending renames after applying
pending_renames.clear()
# Apply pending merges
for merge_operation in pending_merges:
group_names = merge_operation['groups']
new_group_name = merge_operation['new_group']
if new_group_name in groups:
# Append to existing group
merged_files = groups[new_group_name]
print(f"Merging into existing group '{new_group_name}'.") # Debugging statement
else:
# Create new group
merged_files = []
groups[new_group_name] = merged_files
print(f"Creating new group '{new_group_name}' for merging.") # Debugging statement
for group_name in group_names:
if group_name not in groups:
continue
merged_files.extend(groups[group_name])
del groups[group_name]
print(f"Merging group '{group_name}' into '{new_group_name}'") # Debugging statement
# Clear pending merges after applying
pending_merges.clear()
for folder_display_name, group_name in display_to_group_map.items():
files = groups.get(group_name, [])
if grouping_enabled and group_name != "All Files":
# Grouping Enabled and multiple groups
if capitalize_names:
base_folder_name = capitalize_folder_name(group_name)
else:
base_folder_name = group_name
if add_prefix:
if base_folder_name:
# Prefix is added after capitalization
modified_folder_name = f"{parent_prefix} - {base_folder_name}"
else:
# If group folder name is empty, only prefix
modified_folder_name = parent_prefix
else:
modified_folder_name = base_folder_name
new_folder_path = os.path.join(folder_path, modified_folder_name)
# Prevent creating a folder with the same name as the parent folder
if os.path.abspath(new_folder_path) == os.path.abspath(folder_path):
messagebox.showerror("Error", f"Cannot create a folder with the same name as the parent folder: {modified_folder_name}")
print(f"Cannot create folder with the same name as parent: {modified_folder_name}") # Debugging statement
continue
os.makedirs(new_folder_path, exist_ok=True)
print(f"Created folder: {new_folder_path}") # Debugging statement
if group_files and file_num:
subgroups = split_into_subgroups(files, file_num)
for idx, subgroup in enumerate(subgroups, 1):
subfolder_name = f"{modified_folder_name} - {idx}"
subfolder_path = os.path.join(new_folder_path, subfolder_name)
os.makedirs(subfolder_path, exist_ok=True)
print(f"Created subfolder: {subfolder_path}") # Debugging statement
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
print(f"Moved '{file}' to '{subfolder_path}'") # Debugging statement
else:
for file in files:
src = os.path.join(folder_path, file)
dest = os.path.join(new_folder_path, file)
shutil.move(src, dest)
print(f"Moved '{file}' to '{new_folder_path}'") # Debugging statement
elif not grouping_enabled and group_name == "All Files":
# Grouping Disabled: Create subfolders based on file_num directly in the parent directory
if group_files and file_num:
subgroups = split_into_subgroups(files, file_num)
for idx, subgroup in enumerate(subgroups, 1):
subfolder_name = f"{parent_prefix} - {idx}"
subfolder_path = os.path.join(folder_path, subfolder_name)
# Prevent creating a folder with the same name as the parent folder
if os.path.abspath(subfolder_path) == os.path.abspath(folder_path):
messagebox.showerror("Error", f"Cannot create a subfolder with the same name as the parent folder: {subfolder_name}")
print(f"Cannot create subfolder with the same name as parent: {subfolder_name}") # Debugging statement
continue
os.makedirs(subfolder_path, exist_ok=True)
print(f"Created subfolder: {subfolder_path}") # Debugging statement
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
print(f"Moved '{file}' to '{subfolder_path}'") # Debugging statement
elif not group_files:
# If grouping is disabled and file grouping is not enabled,
# and all files are already in the parent folder, do nothing
print("Grouping disabled and file grouping not enabled; no action taken.") # Debugging statement
pass
else:
# Edge Case: grouping_enabled but only "All Files" group exists
# Treat it as grouping disabled
if group_files and file_num:
subgroups = split_into_subgroups(files, file_num)
for idx, subgroup in enumerate(subgroups, 1):
subfolder_name = f"{parent_prefix} - {idx}"
subfolder_path = os.path.join(folder_path, subfolder_name)
# Prevent creating a folder with the same name as the parent folder
if os.path.abspath(subfolder_path) == os.path.abspath(folder_path):
messagebox.showerror("Error", f"Cannot create a subfolder with the same name as the parent folder: {subfolder_name}")
print(f"Cannot create subfolder with the same name as parent: {subfolder_name}") # Debugging statement
continue
os.makedirs(subfolder_path, exist_ok=True)
print(f"Created subfolder: {subfolder_path}") # Debugging statement
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
print(f"Moved '{file}' to '{subfolder_path}'") # Debugging statement
elif not group_files:
# Move all files into a single subfolder named with parent prefix
subfolder_name = f"{parent_prefix}"
subfolder_path = os.path.join(folder_path, subfolder_name)
# Prevent creating a folder with the same name as the parent folder
if os.path.abspath(subfolder_path) == os.path.abspath(folder_path):
# If the subfolder name is the same as the parent folder, skip creation
print(f"Subfolder name same as parent; skipping creation: {subfolder_name}") # Debugging statement
pass
else:
os.makedirs(subfolder_path, exist_ok=True)
print(f"Created subfolder: {subfolder_path}") # Debugging statement
for file in files:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
print(f"Moved '{file}' to '{subfolder_path}'") # Debugging statement
messagebox.showinfo("Success", "Files sorted and organized successfully!")
load_directory() # Reload to reflect changes without opening the folder
print("Files sorted and organized successfully.") # Debugging statement
except Exception as e:
messagebox.showerror("Error", f"An error occurred during sorting:\n{str(e)}")
print(f"Error in execute_sorting: {str(e)}")
Explanation of Changes:
Check if new_group_name Exists:
If new_group_name already exists in groups, append the merged files to the existing group instead of overwriting it.
This allows merging into an existing folder without triggering the duplicate name error.
Merge Logic Adjustment:
When merging, if the destination group (new_group_name) exists, retrieve its current file list and extend it with files from the groups being merged.
This ensures that existing files in the target group are preserved and new files are added appropriately.
def rename_selected_folder():
"""Rename the selected folder directly in the listbox."""
selected_indices = folder_listbox.curselection()
if len(selected_indices) != 1:
messagebox.showinfo("Rename Folder", "Please select exactly one folder to rename.")
print("Rename operation aborted: Incorrect number of selections.") # Debugging statement
return
index = selected_indices[0]
old_display_name = folder_listbox.get(index)
original_group_name = display_to_group_map.get(old_display_name, old_display_name)
print(f"Renaming folder: {original_group_name}") # Debugging statement
# Create an Entry widget overlaying the listbox item
entry = tk.Entry(frame, width=40)
entry.insert(0, old_display_name)
entry.select_range(0, tk.END)
entry.focus()
# Get the coordinates of the selected item
bbox = folder_listbox.bbox(index)
if bbox:
entry.place(x=folder_listbox.winfo_x() + bbox[0], y=folder_listbox.winfo_y() + bbox[1], width=bbox[2], height=bbox[3])
print(f"Entry widget placed at x={folder_listbox.winfo_x() + bbox[0]}, y={folder_listbox.winfo_y() + bbox[1]}") # Debugging statement
def on_enter(event=None):
new_display_name = entry.get().strip()
print(f"New display name entered: {new_display_name}") # Debugging statement
if not new_display_name:
messagebox.showerror("Invalid Name", "Folder name cannot be empty.")
print("Rename operation aborted: Empty name.") # Debugging statement
return
if new_display_name in display_to_group_map and new_display_name != old_display_name:
messagebox.showerror("Duplicate Name", "A folder with this name already exists.")
print("Rename operation aborted: Duplicate name.") # Debugging statement
return
# Register the pending rename
pending_renames[original_group_name] = new_display_name
print(f"Registered pending rename: '{original_group_name}' -> '{new_display_name}'") # Debugging statement
# Update the listbox to reflect the pending rename
folder_listbox.delete(index)
folder_listbox.insert(index, new_display_name)
display_to_group_map[new_display_name] = original_group_name
del display_to_group_map[old_display_name]
print(f"Updated listbox and mapping with pending rename '{new_display_name}'") # Debugging statement
entry.destroy()
# Update the preview to reflect the rename
show_folder_contents()
print(f"Pending rename '{original_group_name}' -> '{new_display_name}' displayed.") # Debugging statement
# Bind Enter key to submit the rename
entry.bind("<Return>", on_enter)
# Bind clicking anywhere else to submit the rename
def on_click_anywhere(event):
widget = event.widget
if widget != entry:
on_enter()
root.bind("<Button-1>", on_click_anywhere)
# Ensure that clicking outside destroys the entry widget
def on_focus_out(event):
on_enter()
entry.bind("<FocusOut>", on_focus_out)
Explanation of Changes:
Duplicate Name Check Adjustment:
Changed the condition to allow renaming a folder to its current name without triggering the duplicate name error. This prevents unnecessary errors when the user doesn't intend to rename.
python
if new_display_name in display_to_group_map and new_display_name != old_display_name:
Ensuring Correct Mapping:
After renaming, display_to_group_map[new_display_name] correctly points to original_group_name, ensuring that the contents are associated correctly.
This mapping is crucial for show_folder_contents to display the appropriate files when the renamed folder is selected.
Verify show_folder_contents Function:
Ensure that show_folder_contents correctly retrieves files based on the updated display_to_group_map, including any pending renames.
python
def show_folder_contents(event=None):
"""Display files or subfolders in the selected group (Preview)."""
try:
selection = folder_listbox.curselection()
if not selection:
content_listbox.delete(0, tk.END)
print("No folder selected.") # Debugging statement
return
folder_display_name = folder_listbox.get(selection[0])
original_group_name = display_to_group_map.get(folder_display_name, folder_display_name)
print(f"Displaying contents of: {original_group_name}") # Debugging statement
# Clear previous contents
content_listbox.delete(0, tk.END)
# Get the list of files in the selected group, considering pending operations
files_in_group = groups.get(original_group_name, [])
# Apply pending renames or merges for preview
preview_files = files_in_group.copy()
if original_group_name in pending_renames:
new_group_name = pending_renames[original_group_name]
print(f"Preview Rename: '{original_group_name}' will be renamed to '{new_group_name}'") # Debugging
if not preview_files:
content_listbox.insert(tk.END, "No files in this group.")
print("No files in this group.") # Debugging statement
return
if group_files_var.get() and enable_grouping_var.get():
file_num_str = file_number_var.get().strip()
if file_num_str:
try:
file_num = int(file_num_str)
if file_num <= 0:
raise ValueError
except ValueError:
messagebox.showerror("Invalid Input", "Please enter a valid positive integer for file grouping.")
content_listbox.delete(0, tk.END) # Clear contents if invalid input
print("Invalid file_num entered.") # Debugging statement
return
# Calculate subgroups based on the file_num
subgroups = split_into_subgroups(preview_files, file_num)
for idx, subgroup in enumerate(subgroups, 1):
# Update subfolder naming to include a hyphen separator
subfolder_display_name = f"{folder_display_name} - {idx}"
content_listbox.insert(tk.END, f"{subfolder_display_name}:")
for file in subgroup:
content_listbox.insert(tk.END, f" - {file}")
elif group_files_var.get() and not enable_grouping_var.get():
file_num_str = file_number_var.get().strip()
if file_num_str:
try:
file_num = int(file_num_str)
if file_num <= 0:
raise ValueError
except ValueError:
messagebox.showerror("Invalid Input", "Please enter a valid positive integer for file grouping.")
content_listbox.delete(0, tk.END) # Clear contents if invalid input
print("Invalid file_num entered.") # Debugging statement
return
# Calculate subgroups based on the file_num
subgroups = split_into_subgroups(preview_files, file_num)
for idx, subgroup in enumerate(subgroups, 1):
# Use parent folder name as the base for subfolder naming
subfolder_display_name = f"{parent_name_var.get().strip()} - {idx}"
content_listbox.insert(tk.END, f"{subfolder_display_name}:")
for file in subgroup:
content_listbox.insert(tk.END, f" - {file}")
else:
for file in preview_files:
content_listbox.insert(tk.END, file)
except Exception as e:
messagebox.showerror("Error", f"An error occurred while displaying folder contents:\n{str(e)}")
print(f"Error in show_folder_contents: {str(e)}")
Explanation of Verification:
The show_folder_contents function relies on display_to_group_map to retrieve the correct original_group_name.
After renaming, display_to_group_map should accurately map the new display name to the corresponding group, ensuring that files_in_group retrieves the correct file list.
By ensuring the rename_selected_folder correctly updates display_to_group_map, the show_folder_contents should function as expected.
def execute_sorting():
"""Execute the sorting: move files into organized folders and subfolders based on selected options."""
if not folder_path or not groups:
messagebox.showerror("Error", "No directory loaded or no files grouped. Please load a directory first.")
print("Execute Sorting failed: No directory loaded or no files grouped.") # Debugging statement
return
try:
parent_prefix = parent_name_var.get().strip()
add_prefix = prefix_var.get()
group_files = group_files_var.get()
capitalize_names = capitalize_var.get()
grouping_enabled = enable_grouping_var.get()
file_num_str = file_number_var.get().strip()
# Determine file_num only if group_files is enabled and file_num_str is not empty
if group_files and file_num_str:
try:
file_num = int(file_num_str)
if file_num <= 0:
raise ValueError
except ValueError:
messagebox.showerror("Invalid Input", "Please enter a valid positive integer for file grouping.")
print("Invalid file_num entered during execute_sorting.") # Debugging statement
return
else:
file_num = None # No grouping by file number
# Apply pending renames
for original_group, new_group in pending_renames.items():
if original_group not in groups:
continue
# Update the group name
groups[new_group] = groups.pop(original_group)
print(f"Renaming group from '{original_group}' to '{new_group}'") # Debugging statement
# Clear pending renames after applying
pending_renames.clear()
# Apply pending merges
for merge_operation in pending_merges:
group_names = merge_operation['groups']
new_group_name = merge_operation['new_group']
if new_group_name in groups:
# Append to existing group
merged_files = groups[new_group_name]
print(f"Merging into existing group '{new_group_name}'.") # Debugging statement
else:
# Create new group
merged_files = []
groups[new_group_name] = merged_files
print(f"Creating new group '{new_group_name}' for merging.") # Debugging statement
for group_name in group_names:
if group_name not in groups:
continue
merged_files.extend(groups[group_name])
del groups[group_name]
print(f"Merging group '{group_name}' into '{new_group_name}'") # Debugging statement
# Clear pending merges after applying
pending_merges.clear()
for folder_display_name, group_name in display_to_group_map.items():
files = groups.get(group_name, [])
if grouping_enabled and group_name != "All Files":
# Grouping Enabled and multiple groups
if capitalize_names:
base_folder_name = capitalize_folder_name(group_name)
else:
base_folder_name = group_name
if add_prefix:
if base_folder_name:
# Prefix is added after capitalization
modified_folder_name = f"{parent_prefix} - {base_folder_name}"
else:
# If group folder name is empty, only prefix
modified_folder_name = parent_prefix
else:
modified_folder_name = base_folder_name
new_folder_path = os.path.join(folder_path, modified_folder_name)
# Prevent creating a folder with the same name as the parent folder
if os.path.abspath(new_folder_path) == os.path.abspath(folder_path):
messagebox.showerror("Error", f"Cannot create a folder with the same name as the parent folder: {modified_folder_name}")
print(f"Cannot create folder with the same name as parent: {modified_folder_name}") # Debugging statement
continue
os.makedirs(new_folder_path, exist_ok=True)
print(f"Created folder: {new_folder_path}") # Debugging statement
if group_files and file_num:
subgroups = split_into_subgroups(files, file_num)
for idx, subgroup in enumerate(subgroups, 1):
subfolder_name = f"{modified_folder_name} - {idx}"
subfolder_path = os.path.join(new_folder_path, subfolder_name)
os.makedirs(subfolder_path, exist_ok=True)
print(f"Created subfolder: {subfolder_path}") # Debugging statement
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
print(f"Moved '{file}' to '{subfolder_path}'") # Debugging statement
else:
for file in files:
src = os.path.join(folder_path, file)
dest = os.path.join(new_folder_path, file)
shutil.move(src, dest)
print(f"Moved '{file}' to '{new_folder_path}'") # Debugging statement
elif not grouping_enabled and group_name == "All Files":
# Grouping Disabled: Create subfolders based on file_num directly in the parent directory
if group_files and file_num:
subgroups = split_into_subgroups(files, file_num)
for idx, subgroup in enumerate(subgroups, 1):
subfolder_name = f"{parent_prefix} - {idx}"
subfolder_path = os.path.join(folder_path, subfolder_name)
# Prevent creating a folder with the same name as the parent folder
if os.path.abspath(subfolder_path) == os.path.abspath(folder_path):
messagebox.showerror("Error", f"Cannot create a subfolder with the same name as the parent folder: {subfolder_name}")
print(f"Cannot create subfolder with the same name as parent: {subfolder_name}") # Debugging statement
continue
os.makedirs(subfolder_path, exist_ok=True)
print(f"Created subfolder: {subfolder_path}") # Debugging statement
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
print(f"Moved '{file}' to '{subfolder_path}'") # Debugging statement
elif not group_files:
# If grouping is disabled and file grouping is not enabled,
# and all files are already in the parent folder, do nothing
print("Grouping disabled and file grouping not enabled; no action taken.") # Debugging statement
pass
else:
# Edge Case: grouping_enabled but only "All Files" group exists
# Treat it as grouping disabled
if group_files and file_num:
subgroups = split_into_subgroups(files, file_num)
for idx, subgroup in enumerate(subgroups, 1):
subfolder_name = f"{parent_prefix} - {idx}"
subfolder_path = os.path.join(folder_path, subfolder_name)
# Prevent creating a folder with the same name as the parent folder
if os.path.abspath(subfolder_path) == os.path.abspath(folder_path):
messagebox.showerror("Error", f"Cannot create a subfolder with the same name as the parent folder: {subfolder_name}")
print(f"Cannot create subfolder with the same name as parent: {subfolder_name}") # Debugging statement
continue
os.makedirs(subfolder_path, exist_ok=True)
print(f"Created subfolder: {subfolder_path}") # Debugging statement
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
print(f"Moved '{file}' to '{subfolder_path}'") # Debugging statement
elif not group_files:
# Move all files into a single subfolder named with parent prefix
subfolder_name = f"{parent_prefix}"
subfolder_path = os.path.join(folder_path, subfolder_name)
# Prevent creating a folder with the same name as the parent folder
if os.path.abspath(subfolder_path) == os.path.abspath(folder_path):
# If the subfolder name is the same as the parent folder, skip creation
print(f"Subfolder name same as parent; skipping creation: {subfolder_name}") # Debugging statement
pass
else:
os.makedirs(subfolder_path, exist_ok=True)
print(f"Created subfolder: {subfolder_path}") # Debugging statement
for file in files:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
print(f"Moved '{file}' to '{subfolder_path}'") # Debugging statement
messagebox.showinfo("Success", "Files sorted and organized successfully!")
load_directory() # Reload to reflect changes without opening the folder
print("Files sorted and organized successfully.") # Debugging statement
except Exception as e:
messagebox.showerror("Error", f"An error occurred during sorting:\n{str(e)}")
print(f"Error in execute_sorting: {str(e)}")