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
merge_history = [] # Stack to keep track of merges for undo functionality
rename_in_progress = False # Flag to prevent multiple rename operations
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:
return []
code
# 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)
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:
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)
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)
return temp_groups
else:
# Treat all files as a single group named "All Files"
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
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)
return modified_name
def get_preview_groups():
"""
Create a preview of groups by applying pending renames and merges.
code
Returns:
dict: A dictionary representing the preview groups.
"""
# Start with a copy of the current groups
preview = groups.copy()
# Apply pending renames
for original_group, new_group in pending_renames.items():
if original_group in preview:
preview[new_group] = preview.pop(original_group)
# Apply pending merges
for merge_op in pending_merges:
groups_to_merge = merge_op['groups']
new_group_name = merge_op['new_group']
merged_files = []
for group in groups_to_merge:
merged_files.extend(preview.get(group, []))
if group in preview:
del preview[group]
if new_group_name in preview:
preview[new_group_name].extend(merged_files)
else:
preview[new_group_name] = merged_files
return preview
def update_folder_info(preview_groups):
"""Update the informational labels with folder details."""
# Clear previous info
total_files_label.config(text=f"Total Files: {sum(len(files) for files in preview_groups.values())}")
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)
return
code
def load_directory():
"""Load the selected directory, prepare groups, and update the displayed folders."""
global folder_path, groups, pending_renames, pending_merges, merge_history
folder_path = filedialog.askdirectory(title="Select the folder containing your files")
if not folder_path:
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))]
groups = prepare_groups(files, enable_grouping_var.get())
# Reset any pending operations
pending_renames.clear()
pending_merges.clear()
merge_history.clear()
update_displayed_folders()
except Exception as e:
messagebox.showerror("Error", f"An error occurred while loading the directory:\n{str(e)}")
def apply_options(*args):
"""Apply the selected options and update the preview."""
if not folder_path:
return # Do nothing if no directory is loaded
try:
update_displayed_folders()
except Exception as e:
messagebox.showerror("Error", f"An error occurred while applying options:\n{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))]
groups = prepare_groups(files, enable_grouping_var.get())
update_displayed_folders()
except Exception as e:
messagebox.showerror("Error", f"An error occurred while updating the preview:\n{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()
# Get the preview groups
preview_groups = get_preview_groups()
# Update folder info
update_folder_info(preview_groups)
for folder_name in sorted(preview_groups.keys()):
# Determine the display folder name based on current settings and pending renames
display_name = folder_name
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:
# Treat "All Files" 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
# Automatically select the first folder and show its contents
if folder_listbox.size() > 0:
folder_listbox.select_set(0)
show_folder_contents()
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.")
return
code
---------------------------- New Functions ----------------------------
def rename_selected_folder():
"""Rename the selected folder directly in the listbox."""
global rename_in_progress
selected_indices = folder_listbox.curselection()
if len(selected_indices) != 1:
messagebox.showinfo("Rename Folder", "Please select exactly one folder to rename.")
return
code
if rename_in_progress:
messagebox.showinfo("Rename in Progress", "Please complete the current rename operation before starting a new one.")
return
rename_in_progress = True
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)
# 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])
def on_enter(event=None):
global rename_in_progress
new_display_name = entry.get().strip()
if not new_display_name:
messagebox.showerror("Invalid Name", "Folder name cannot be empty.")
rename_in_progress = False
entry.destroy()
root.unbind("<Button-1>")
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.")
rename_in_progress = False
entry.destroy()
root.unbind("<Button-1>")
return
# Register the pending rename
pending_renames[original_group_name] = new_display_name
# 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]
entry.destroy()
rename_in_progress = False
# Unbind the <Button-1> event
root.unbind("<Button-1>")
# Update the preview to reflect the rename
update_displayed_folders()
# 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.")
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]
# 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)
if not new_folder_name:
messagebox.showerror("Invalid Name", "Folder name cannot be empty.")
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.")
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.")
return
# Register the pending merge
pending_merges.append({'groups': selected_group_names, 'new_group': new_folder_name})
# Push to merge history for undo functionality
merge_history.append({'groups': selected_group_names, 'new_group': new_folder_name})
# 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]
# 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
# Update the preview to reflect the merge
update_displayed_folders()
def undo_merge():
"""Undo the last merge operation."""
if not merge_history:
messagebox.showinfo("Undo Merge", "No merge operations to undo.")
return
code
last_merge = merge_history.pop()
groups_to_split = last_merge['groups']
new_group_name = last_merge['new_group']
# Remove the merged group from pending_merges
pending_merges[:] = [merge for merge in pending_merges if merge['new_group'] != new_group_name]
# Re-add the original groups to pending merges
for group in groups_to_split:
# Find the original folder name before merge
original_group_name = group # Assuming group names are original
# Re-add as individual groups
pending_renames.pop(original_group_name, None) # Remove any pending renames on these groups if necessary
# Update the listbox to reflect the undone merge
folder_listbox.delete(tk.END) # Remove the merged folder
for group in groups_to_split:
folder_listbox.insert(tk.END, group)
display_to_group_map[group] = group
# Remove the merged folder display mapping
if new_group_name in display_to_group_map:
del display_to_group_map[new_group_name]
# Update the preview to reflect the undone merge
update_displayed_folders()
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)
# Add Undo Merge option if there are merges to undo
if merge_history:
context_menu.add_separator()
context_menu.add_command(label="Undo Last Merge", command=undo_merge)
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)}")
When the Folder Grouping is disable and Group by file number is enable, a repeated Parent Folder name is seen in the Group Folders Listbox. I want all the subfolders created using the Group by file number display inside the Grouped Folders Listbox. Is it a easy task?
Assistant
Yes, it's possible to adjust your code to display subfolders derived from the "Group by file number" option when folder grouping is disabled. You need to modify the logic that populates the folder_listbox when updating the displayed folders.
Here's how you can address this:
Inside update_displayed_folders(), introduce logic to handle the case when grouping is disabled but file splitting is enabled. This involves creating virtual subgroups based on the file number and updating the folder_listbox with these instead of a single entry.
Modify the show_folder_contents function to handle clicking on these subfolder entries and displaying their contents correctly.
Here's the modification in code to handle the display correctly:
python
User
so there is no need for the def update_folder_info(preview_groups):?
Assistant
The update_folder_info(preview_groups) function is used to update labels or other interface elements to reflect the total number of files and possibly other statistics based on the current state of the grouped files. If your application UI uses this information (like displaying a total file count), maintaining this function can be useful. However, if such information isn't necessary or isn't displayed anywhere in your UI, then you can simplify the code by removing this function.
If you choose to keep it, you may want to update it slightly to ensure it fits the newly adapted logic, especially if you're dealing with subgroups or similar entities.
Here’s what the simplified version of the function might look like if you still need it for the file count:
python
def update_folder_info(preview_groups):
"""Update the informational labels with folder details."""
# Calculate total number of files across all preview groups
total_files_count = sum(len(files) for group in preview_groups.values() for files in (group if isinstance(group, list) else [group]))
total_files_label.config(text=f"Total Files: {total_files_count}")
Adjustments
File Count Calculation: This totals up files across all groups or subgroups, accommodating the potential changes you made regarding how files are distributed into subgroups.
If you find that the total file count or similar summaries are not critical to your application, you can decide to remove the function entirely. The choice depends on your application's user interface needs and how you aim to inform users about current data states.
User
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
merge_history = [] # Stack to keep track of merges for undo functionality
rename_in_progress = False # Flag to prevent multiple rename operations
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:
return []
code
Assistant
To address your concern about not creating subfolders when the number of files is less than the specified "Files per subfolder" value, we can adjust the logic in the execute_sorting function. Specifically, we'll only create subfolders if the number of files exceeds the specified "Files per subfolder" value. Otherwise, we'll place the files directly into the primary folder.
Here's how you can modify the execute_sorting function to achieve this:
python
User
Now let's address the display problem with the disabling Grouping Folder.
I have 998 needs to split into subfolders. When I try to do that, it will display the right amount of subfolders. However, many of them are repeating (indexing only went up to 7). The content of the first indexed folder only has 7 files.
Assistant
To address the issue with repeating folder names and inconsistent subfolder contents when folder grouping is disabled but "Group by file number" is enabled, we need to ensure the logic correctly splits the files into subgroups and that each subgroup gets a unique and correctly indexed folder name.
Let's go through the needed corrections:
Ensure Correct Subgroup Calculation: Verify that subgroups are calculated properly and that no files are left in the main list beyond what is intended.
Display Names and Mapping: Make sure that each subgroup is assigned a unique name and that the listbox mapping properly reflects that name.
Here is an updated approach to fix the subgroup counting and ensure proper display in the listbox:
Updating update_displayed_folders
Ensure that when displaying subgroups in the listbox, each subgroup is given a distinct and sequential index by correctly calculating all possible subgroups:
python
User
I get "An error occurred while displaying folder contents: unhashable type:'list' error message
Assistant
The error message "unhashable type: 'list'" typically occurs in Python when you mistakenly try to use a list as a key in a dictionary or add a list to a set. In your code, the logic might inadvertently be misusing a list where a string or another hashable type like a tuple was expected.
In the context of the previous code, when you map subfolder_name to display_to_group_map, ensure that it's being mapped to something hashable (like a tuple, not a list). Let's ensure the mapping logic is correct.
Here's an updated explanation with corrections to avoid the "unhashable" error:
Correct Mapping and Display Logic
Use Tuples for Mapping: When storing associations in display_to_group_map, ensure you use hashable data types like strings or tuples rather than lists.
Adjust display_to_group_map Storage: Use tuples where necessary to maintain any required context without triggering hashing issues.
Below is the modified section to map display_to_group_map appropriately, using tuples:
python
User
The error is gone but problem with the repeating subfolder remains. Also I have noticed the Total Files information is replaced by Files in this Subfolder. I would like to move the Files in this Subfolder back to its original position (between filename listbox and Execute Sorting button) and bring back the Total Files information after the extry box.
Assistant
To address the issue with repeating subfolders and also reposition the informational labels for "Total Files" and "Files in this Subfolder", we need to focus on the following areas:
Ensure Unique Subfolder Display and Content: Resolve repeating issues by correctly indexing subfolders and managing subgroups properly.
Informational Label Placement and Data: Reintroduce the "Total Files" label next to the entry box and correctly calculate the number of files in each selected subgroup between the listbox and the execution button.
Here's how you can implement these:
Step-by-Step Fix and Enhancements
1. Fix Repeating Subfolders
Ensure the indices are incremented properly and you handle each file group or subgroup separately:
python
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()
folder_listbox.delete(0, tk.END)
display_to_group_map.clear()
preview_groups = get_preview_groups()
update_folder_info(preview_groups)
for folder_name in sorted(preview_groups.keys()):
files = preview_groups.get(folder_name, [])
if not enable_grouping_var.get() and group_files_var.get():
file_num_str = file_number_var.get().strip()
try:
file_num = int(file_num_str) if file_num_str else None
except ValueError:
continue
if file_num and len(files) > 0:
subgroups = split_into_subgroups(files, file_num)
for idx, subgroup in enumerate(subgroups, 1):
subfolder_name = f"{parent_prefix} - {idx}"
folder_listbox.insert(tk.END, subfolder_name)
display_to_group_map[subfolder_name] = tuple(subgroup)
else:
folder_listbox.insert(tk.END, parent_prefix)
display_to_group_map[parent_prefix] = tuple(files)
else:
if capitalize_names:
modified_folder_name = capitalize_folder_name(folder_name)
else:
modified_folder_name = folder_name
if add_prefix:
if modified_folder_name:
modified_folder_name = f"{parent_prefix} - {modified_folder_name}"
else:
modified_folder_name = parent_prefix
folder_listbox.insert(tk.END, modified_folder_name)
display_to_group_map[modified_folder_name] = tuple(files)
if folder_listbox.size() > 0:
folder_listbox.select_set(0)
show_folder_contents()
User
can you print out the entire script?
Assistant
Certainly! Here is the complete script with the required modifications to address the issues you mentioned:
python
User
the merging function is not in correct. This script has the correct merging function.
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
merge_history = [] # Stack to keep track of merges for undo functionality
rename_in_progress = False # Flag to prevent multiple rename operations
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:
return []
Assistant
Sure! To ensure that the merging functionality works correctly, let's integrate the improved logic into your existing code. The goal is to ensure that you can merge selected folders correctly, maintaining accurate representation and functionality.
Here is the corrected and full implementation that includes the merging logic as provided and any necessary adaptation to ensure consistent behavior:
python
User
When I merge the folders, the new folder is content is the folder name display vertically (one letter per line)
Everything in the given script should be correct. Please use it as the backbone.
Assistant
The issue you're describing sounds like the new folder display name (after merging) is being incorrectly split into individual characters or treated as individual lines. This typically suggests that somewhere in the code, probably during the update or display process, the string representation of the folder name is misused.
Let's address this problem ensuring the merging process uses and updates folder contents correctly, without splitting the strings inappropriately. I'll provide the fixed version based on the backbone of your existing script:
folder_display_name = folder_listbox.get(selection[0])
original_group_name = display_to_group_map.get(folder_display_name, folder_display_name)
# Get the preview groups
preview_groups = get_preview_groups()
# Update folder info
update_folder_info(preview_groups)
# Clear previous contents
content_listbox.delete(0, tk.END)
# Get the list of files in the selected group, considering pending operations
files_in_group = preview_groups.get(original_group_name, [])
if not files_in_group:
content_listbox.insert(tk.END, "No files in this group.")
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
return
# Calculate subgroups based on the file_num
subgroups = split_into_subgroups(files_in_group, 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 files_in_group:
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
return
# Calculate subgroups based on the file_num
subgroups = split_into_subgroups(files_in_group, 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 files_in_group:
content_listbox.insert(tk.END, file)
else:
for file in files_in_group:
content_listbox.insert(tk.END, file)
# Display file number info for Grouped Folders
if enable_grouping_var.get():
file_number_info.config(text=f"Files in this Group: {len(files_in_group)}")
else:
file_number_info.config(text="")
except Exception as e:
messagebox.showerror("Error", f"An error occurred while displaying folder contents:\n{str(e)}")
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.")
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)
# 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]
else:
# Create new group
merged_files = []
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]
# Push to merge history for undo functionality
merge_history.append(merge_operation)
# Clear pending merges after applying
pending_merges.clear()
# Now proceed with file moving based on updated groups
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}")
continue
os.makedirs(new_folder_path, exist_ok=True)
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)
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
else:
for file in files:
src = os.path.join(folder_path, file)
dest = os.path.join(new_folder_path, file)
shutil.move(src, dest)
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}")
continue
os.makedirs(subfolder_path, exist_ok=True)
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
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
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}")
continue
os.makedirs(subfolder_path, exist_ok=True)
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
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
pass
else:
os.makedirs(subfolder_path, exist_ok=True)
for file in files:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
messagebox.showinfo("Success", "Files sorted and organized successfully!")
load_directory() # Reload to reflect changes without opening the folder
except Exception as e:
messagebox.showerror("Error", f"An error occurred during sorting:\n{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()
folder_listbox.delete(0, tk.END)
display_to_group_map.clear()
# Get the preview groups
preview_groups = get_preview_groups()
# Update folder info
update_folder_info(preview_groups)
for folder_name in sorted(preview_groups.keys()):
# When grouping is disabled and "Group by file number" is enabled,
# create virtual subgroups for display
if not enable_grouping_var.get() and group_files_var.get():
file_num_str = file_number_var.get().strip()
try:
file_num = int(file_num_str) if file_num_str else None
except ValueError:
continue
files = preview_groups.get(folder_name, [])
if file_num:
subgroups = split_into_subgroups(files, file_num)
for idx, subgroup in enumerate(subgroups, 1):
subfolder_name = f"{parent_prefix} - {idx}"
folder_listbox.insert(tk.END, subfolder_name)
display_to_group_map[subfolder_name] = (folder_name, idx, subgroup) # Map uses a tuple for subgroups
else:
folder_listbox.insert(tk.END, parent_prefix)
display_to_group_map[parent_prefix] = folder_name
else:
# Regular processing for enabled grouping
display_name = folder_name
if capitalize_names:
modified_folder_name = capitalize_folder_name(display_name)
else:
modified_folder_name = display_name
if add_prefix:
if modified_folder_name:
modified_folder_name = f"{parent_prefix} - {modified_folder_name}"
else:
modified_folder_name = parent_prefix
folder_listbox.insert(tk.END, modified_folder_name)
display_to_group_map[modified_folder_name] = folder_name
# Automatically select the first folder and show its contents
if folder_listbox.size() > 0:
folder_listbox.select_set(0)
show_folder_contents()
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)
return
folder_display_name = folder_listbox.get(selection[0])
mapping = display_to_group_map.get(folder_display_name, folder_display_name)
# Get the preview groups
preview_groups = get_preview_groups()
# Update folder info
update_folder_info(preview_groups)
# Clear previous contents
content_listbox.delete(0, tk.END)
if isinstance(mapping, tuple):
# When using file number grouping with group_files_var enabled and enable_grouping_var disabled
files_in_group = mapping[2] # This is the subgroup from the mapping tuple
else:
# For regular groups
files_in_group = preview_groups.get(mapping, [])
if not files_in_group:
content_listbox.insert(tk.END, "No files in this group.")
return
for file in files_in_group:
content_listbox.insert(tk.END, file)
# Display file number info if grouping is supported
if enable_grouping_var.get():
file_number_info.config(text=f"Files in this Group: {len(files_in_group)}")
else:
file_number_info.config(text="")
except Exception as e:
messagebox.showerror("Error", f"An error occurred while displaying folder contents:\n{str(e)}")
Key Adjustments:
Tuple Mapping for Display Names: When disabling grouping but enabling file-based grouping, map the display names directly to a tuple containing (original_group_name, subgroup_index, subgroup_files) to preserve context.
Content Display Logic: Adjust show_folder_contents to recognize when a selection in the listbox corresponds to a subgroup versus a regular group.
These changes should help your GUI reflect subfolders accurately whenever "Group by file number" is active, without producing redundant entries.
# 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)
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:
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)
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)
return temp_groups
else:
# Treat all files as a single group named "All Files"
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
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)
return modified_name
def get_preview_groups():
"""
Create a preview of groups by applying pending renames and merges.
code
Returns:
dict: A dictionary representing the preview groups.
"""
# Start with a copy of the current groups
preview = groups.copy()
# Apply pending renames
for original_group, new_group in pending_renames.items():
if original_group in preview:
preview[new_group] = preview.pop(original_group)
# Apply pending merges
for merge_op in pending_merges:
groups_to_merge = merge_op['groups']
new_group_name = merge_op['new_group']
merged_files = []
for group in groups_to_merge:
merged_files.extend(preview.get(group, []))
if group in preview:
del preview[group]
if new_group_name in preview:
preview[new_group_name].extend(merged_files)
else:
preview[new_group_name] = merged_files
return preview
def update_folder_info(preview_groups):
"""Update the informational labels with folder details."""
# Clear previous info
total_files_label.config(text=f"Total Files: {sum(len(files) for files in preview_groups.values())}")
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)
return
code
folder_display_name = folder_listbox.get(selection[0])
original_group_name = display_to_group_map.get(folder_display_name, folder_display_name)
# Get the preview groups
preview_groups = get_preview_groups()
# Update folder info
update_folder_info(preview_groups)
# Clear previous contents
content_listbox.delete(0, tk.END)
# Get the list of files in the selected group, considering pending operations
files_in_group = preview_groups.get(original_group_name, [])
if not files_in_group:
content_listbox.insert(tk.END, "No files in this group.")
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
return
# Calculate subgroups based on the file_num
subgroups = split_into_subgroups(files_in_group, 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 files_in_group:
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
return
# Calculate subgroups based on the file_num
subgroups = split_into_subgroups(files_in_group, 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 files_in_group:
content_listbox.insert(tk.END, file)
else:
for file in files_in_group:
content_listbox.insert(tk.END, file)
# Display file number info for Grouped Folders
if enable_grouping_var.get():
file_number_info.config(text=f"Files in this Group: {len(files_in_group)}")
else:
file_number_info.config(text="")
except Exception as e:
messagebox.showerror("Error", f"An error occurred while displaying folder contents:\n{str(e)}")
def load_directory():
"""Load the selected directory, prepare groups, and update the displayed folders."""
global folder_path, groups, pending_renames, pending_merges, merge_history
folder_path = filedialog.askdirectory(title="Select the folder containing your files")
if not folder_path:
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))]
groups = prepare_groups(files, enable_grouping_var.get())
# Reset any pending operations
pending_renames.clear()
pending_merges.clear()
merge_history.clear()
update_displayed_folders()
except Exception as e:
messagebox.showerror("Error", f"An error occurred while loading the directory:\n{str(e)}")
def apply_options(*args):
"""Apply the selected options and update the preview."""
if not folder_path:
return # Do nothing if no directory is loaded
try:
update_displayed_folders()
except Exception as e:
messagebox.showerror("Error", f"An error occurred while applying options:\n{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))]
groups = prepare_groups(files, enable_grouping_var.get())
update_displayed_folders()
except Exception as e:
messagebox.showerror("Error", f"An error occurred while updating the preview:\n{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()
# Get the preview groups
preview_groups = get_preview_groups()
# Update folder info
update_folder_info(preview_groups)
for folder_name in sorted(preview_groups.keys()):
# When grouping is disabled and "Group by file number" is enabled,
# create virtual subgroups for display
if not enable_grouping_var.get() and group_files_var.get():
file_num_str = file_number_var.get().strip()
try:
file_num = int(file_num_str) if file_num_str else None
except ValueError:
continue
files = preview_groups.get(folder_name, [])
if file_num:
subgroups = split_into_subgroups(files, file_num)
for idx, subgroup in enumerate(subgroups, 1):
subfolder_name = f"{parent_prefix} - {idx}"
folder_listbox.insert(tk.END, subfolder_name)
display_to_group_map[subfolder_name] = (folder_name, idx, subgroup) # Map uses a tuple for subgroups
else:
folder_listbox.insert(tk.END, parent_prefix)
display_to_group_map[parent_prefix] = folder_name
else:
# Regular processing for enabled grouping
display_name = folder_name
if capitalize_names:
modified_folder_name = capitalize_folder_name(display_name)
else:
modified_folder_name = display_name
if add_prefix:
if modified_folder_name:
modified_folder_name = f"{parent_prefix} - {modified_folder_name}"
else:
modified_folder_name = parent_prefix
folder_listbox.insert(tk.END, modified_folder_name)
display_to_group_map[modified_folder_name] = folder_name
# Automatically select the first folder and show its contents
if folder_listbox.size() > 0:
folder_listbox.select_set(0)
show_folder_contents()
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)
return
code
folder_display_name = folder_listbox.get(selection[0])
mapping = display_to_group_map.get(folder_display_name, folder_display_name)
# Get the preview groups
preview_groups = get_preview_groups()
# Update folder info
update_folder_info(preview_groups)
# Clear previous contents
content_listbox.delete(0, tk.END)
if isinstance(mapping, tuple):
# When using file number grouping with group_files_var enabled and enable_grouping_var disabled
files_in_group = mapping[2] # This is the subgroup from the mapping tuple
else:
# For regular groups
files_in_group = preview_groups.get(mapping, [])
if not files_in_group:
content_listbox.insert(tk.END, "No files in this group.")
return
for file in files_in_group:
content_listbox.insert(tk.END, file)
# Display file number info if grouping is supported
if enable_grouping_var.get():
file_number_info.config(text=f"Files in this Group: {len(files_in_group)}")
else:
file_number_info.config(text="")
except Exception as e:
messagebox.showerror("Error", f"An error occurred while displaying folder contents:\n{str(e)}")
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.")
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.")
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)
# 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]
else:
# Create new group
merged_files = []
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]
# Push to merge history for undo functionality
merge_history.append(merge_operation)
# Clear pending merges after applying
pending_merges.clear()
# Now proceed with file moving based on updated groups
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}")
continue
os.makedirs(new_folder_path, exist_ok=True)
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)
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
else:
for file in files:
src = os.path.join(folder_path, file)
dest = os.path.join(new_folder_path, file)
shutil.move(src, dest)
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}")
continue
os.makedirs(subfolder_path, exist_ok=True)
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
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
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}")
continue
os.makedirs(subfolder_path, exist_ok=True)
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
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
pass
else:
os.makedirs(subfolder_path, exist_ok=True)
for file in files:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
messagebox.showinfo("Success", "Files sorted and organized successfully!")
load_directory() # Reload to reflect changes without opening the folder
except Exception as e:
messagebox.showerror("Error", f"An error occurred during sorting:\n{str(e)}")
---------------------------- New Functions ----------------------------
def rename_selected_folder():
"""Rename the selected folder directly in the listbox."""
global rename_in_progress
selected_indices = folder_listbox.curselection()
if len(selected_indices) != 1:
messagebox.showinfo("Rename Folder", "Please select exactly one folder to rename.")
return
code
if rename_in_progress:
messagebox.showinfo("Rename in Progress", "Please complete the current rename operation before starting a new one.")
return
rename_in_progress = True
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)
# 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])
def on_enter(event=None):
global rename_in_progress
new_display_name = entry.get().strip()
if not new_display_name:
messagebox.showerror("Invalid Name", "Folder name cannot be empty.")
rename_in_progress = False
entry.destroy()
root.unbind("<Button-1>")
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.")
rename_in_progress = False
entry.destroy()
root.unbind("<Button-1>")
return
# Register the pending rename
pending_renames[original_group_name] = new_display_name
# 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]
entry.destroy()
rename_in_progress = False
# Unbind the <Button-1> event
root.unbind("<Button-1>")
# Update the preview to reflect the rename
update_displayed_folders()
# 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.")
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]
# 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)
if not new_folder_name:
messagebox.showerror("Invalid Name", "Folder name cannot be empty.")
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.")
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.")
return
# Register the pending merge
pending_merges.append({'groups': selected_group_names, 'new_group': new_folder_name})
# Push to merge history for undo functionality
merge_history.append({'groups': selected_group_names, 'new_group': new_folder_name})
# 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]
# 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
# Update the preview to reflect the merge
update_displayed_folders()
def undo_merge():
"""Undo the last merge operation."""
if not merge_history:
messagebox.showinfo("Undo Merge", "No merge operations to undo.")
return
code
last_merge = merge_history.pop()
groups_to_split = last_merge['groups']
new_group_name = last_merge['new_group']
# Remove the merged group from pending_merges
pending_merges[:] = [merge for merge in pending_merges if merge['new_group'] != new_group_name]
# Re-add the original groups to pending merges
for group in groups_to_split:
# Find the original folder name before merge
original_group_name = group # Assuming group names are original
# Re-add as individual groups
pending_renames.pop(original_group_name, None) # Remove any pending renames on these groups if necessary
# Update the listbox to reflect the undone merge
folder_listbox.delete(tk.END) # Remove the merged folder
for group in groups_to_split:
folder_listbox.insert(tk.END, group)
display_to_group_map[group] = group
# Remove the merged folder display mapping
if new_group_name in display_to_group_map:
del display_to_group_map[new_group_name]
# Update the preview to reflect the undone merge
update_displayed_folders()
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)
# Add Undo Merge option if there are merges to undo
if merge_history:
context_menu.add_separator()
context_menu.add_command(label="Undo Last Merge", command=undo_merge)
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)}")
First of all I found a mistake in creating subfolders using file number. When the Grouped Folder has less than the Files per subfolder value, it still create a subfolder holding all the file. It should only create a Grouped Folder using the name in the Listbox and not to create any subfolder folding all the content.
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.")
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.")
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)
# 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]
else:
# Create new group
merged_files = []
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]
# Push to merge history for undo functionality
merge_history.append(merge_operation)
# Clear pending merges after applying
pending_merges.clear()
# Now proceed with file moving based on updated groups
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}")
continue
os.makedirs(new_folder_path, exist_ok=True)
if group_files and file_num and len(files) > 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)
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
else:
# Move files directly to the group folder if no subgrouping is needed
for file in files:
src = os.path.join(folder_path, file)
dest = os.path.join(new_folder_path, file)
shutil.move(src, dest)
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 and len(files) > 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}")
continue
os.makedirs(subfolder_path, exist_ok=True)
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
else:
# If no subgrouping is needed, move directly into a single appropriate folder
for file in files:
src = os.path.join(folder_path, file)
dest = os.path.join(folder_path, file)
shutil.move(src, dest)
else:
# Edge Case: Grouping enabled but only "All Files" group exists
# Treat it as grouping disabled
if group_files and file_num and len(files) > 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)
os.makedirs(subfolder_path, exist_ok=True)
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
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 subfolder with the same name as the parent folder
if os.path.abspath(subfolder_path) == os.path.abspath(folder_path):
pass
else:
os.makedirs(subfolder_path, exist_ok=True)
for file in files:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
messagebox.showinfo("Success", "Files sorted and organized successfully!")
load_directory() # Reload to reflect changes without opening the folder
except Exception as e:
messagebox.showerror("Error", f"An error occurred during sorting:\n{str(e)}")
Key Adjustments:
Conditionally Create Subfolders: Adjusted the logic to check if the length of files exceeds file_num before creating subfolders. If it doesn't, the files are moved directly to the main group folder.
This logic ensures that you only create subfolders when actually needed based on the specified number of files per subfolder.
This modification supports your requirement not to create a subfolder if all files can fit into the group folder of the listbox entry directly.
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()
folder_listbox.delete(0, tk.END)
display_to_group_map.clear()
# Get the preview groups
preview_groups = get_preview_groups()
# Update folder info
update_folder_info(preview_groups)
for folder_name in sorted(preview_groups.keys()):
# When grouping is disabled and "Group by file number" is enabled,
# create virtual subgroups for display
if not enable_grouping_var.get() and group_files_var.get():
file_num_str = file_number_var.get().strip()
try:
file_num = int(file_num_str) if file_num_str else None
except ValueError:
continue
files = preview_groups.get(folder_name, [])
if file_num and len(files) > 0:
subgroups = split_into_subgroups(files, file_num)
for idx, subgroup in enumerate(subgroups, 1):
subfolder_name = f"{parent_prefix} - {idx}"
folder_listbox.insert(tk.END, subfolder_name)
display_to_group_map[subfolder_name] = subgroup # Map to the subgroup files
else:
# If files fit into one folder, display as a single group
folder_listbox.insert(tk.END, parent_prefix)
display_to_group_map[parent_prefix] = files
else:
# Regular processing for enabled grouping
display_name = folder_name
if capitalize_names:
modified_folder_name = capitalize_folder_name(display_name)
else:
modified_folder_name = display_name
if add_prefix:
if modified_folder_name:
modified_folder_name = f"{parent_prefix} - {modified_folder_name}"
else:
modified_folder_name = parent_prefix
folder_listbox.insert(tk.END, modified_folder_name)
display_to_group_map[modified_folder_name] = preview_groups[folder_name]
# Automatically select the first folder and show its contents
if folder_listbox.size() > 0:
folder_listbox.select_set(0)
show_folder_contents()
Updates to show_folder_contents
Ensure that it correctly handles these mappings, assuming the mapping could be directly to a file list:
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)
return
folder_display_name = folder_listbox.get(selection[0])
files_in_group = display_to_group_map.get(folder_display_name, [])
# Update folder info
total_files_label.config(text=f"Files in this Subfolder: {len(files_in_group)}")
# Clear previous contents
content_listbox.delete(0, tk.END)
if not files_in_group:
content_listbox.insert(tk.END, "No files in this group.")
return
for file in files_in_group:
content_listbox.insert(tk.END, file)
except Exception as e:
messagebox.showerror("Error", f"An error occurred while displaying folder contents:\n{str(e)}")
Key Points:
Unique Subfolder Names: Each subgroup is assigned a distinct name using an auto-incremented index.
Correct Mapping: Ensure the display_to_group_map directly maps subfolder display names to the correct list of files for that subgroup.
Update and Clean Mapping: Correctly capture the mapping in display_to_group_map to ensure it accurately reflects the subgroup contents and order.
These changes should solve the repeated index issue and ensure the subfolder contents are displayed and indexed correctly when you disable grouping but enable subgrouping by file number.
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()
folder_listbox.delete(0, tk.END)
display_to_group_map.clear()
# Get the preview groups
preview_groups = get_preview_groups()
# Update folder info
update_folder_info(preview_groups)
for folder_name in sorted(preview_groups.keys()):
# When grouping is disabled and "Group by file number" is enabled,
# create virtual subgroups for display
if not enable_grouping_var.get() and group_files_var.get():
file_num_str = file_number_var.get().strip()
try:
file_num = int(file_num_str) if file_num_str else None
except ValueError:
continue
files = preview_groups.get(folder_name, [])
if file_num and len(files) > 0:
subgroups = split_into_subgroups(files, file_num)
for idx, subgroup in enumerate(subgroups, 1):
subfolder_name = f"{parent_prefix} - {idx}"
folder_listbox.insert(tk.END, subfolder_name)
display_to_group_map[subfolder_name] = tuple(subgroup) # Use tuple, not list
else:
# If files fit into one folder, display as a single group
folder_listbox.insert(tk.END, parent_prefix)
display_to_group_map[parent_prefix] = tuple(files)
else:
# Regular processing for enabled grouping
display_name = folder_name
if capitalize_names:
modified_folder_name = capitalize_folder_name(display_name)
else:
modified_folder_name = display_name
if add_prefix:
if modified_folder_name:
modified_folder_name = f"{parent_prefix} - {modified_folder_name}"
else:
modified_folder_name = parent_prefix
folder_listbox.insert(tk.END, modified_folder_name)
display_to_group_map[modified_folder_name] = tuple(preview_groups[folder_name])
# Automatically select the first folder and show its contents
if folder_listbox.size() > 0:
folder_listbox.select_set(0)
show_folder_contents()
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)
return
folder_display_name = folder_listbox.get(selection[0])
files_in_group = list(display_to_group_map.get(folder_display_name, [])) # Convert back to a list to display
# Update folder info
total_files_label.config(text=f"Files in this Subfolder: {len(files_in_group)}")
# Clear previous contents
content_listbox.delete(0, tk.END)
if not files_in_group:
content_listbox.insert(tk.END, "No files in this group.")
return
for file in files_in_group:
content_listbox.insert(tk.END, file)
except Exception as e:
messagebox.showerror("Error", f"An error occurred while displaying folder contents:\n{str(e)}")
Key Adjustments:
Tuple Conversion: When assigning subgroup to display_to_group_map, convert the list into a tuple since tuples are hashable and can be used as dictionary keys.
Conversion Back to List: When displaying contents in show_folder_contents, convert the tuple back to a list for easy iteration and manipulation.
By using tuples for the mapping, you ensure no "unhashable type" errors occur and that the application logic is sound. This also makes sure sets, which require hashable elements, are not unexpectedly involved in list-like operations.
2. Show Correct File Counts
For repositioning and showing the labels, you can adjust their placements in the grid:
python
def update_folder_info(preview_groups):
"""Update the informational labels with folder details."""
total_files_count = sum(len(files) for files in preview_groups.values())
total_files_label.config(text=f"Total Files: {total_files_count}")
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)
file_number_info.config(text="")
return
folder_display_name = folder_listbox.get(selection[0])
files_in_group = list(display_to_group_map.get(folder_display_name, []))
file_number_info.config(text=f"Files in this Subfolder: {len(files_in_group)}")
content_listbox.delete(0, tk.END)
if not files_in_group:
content_listbox.insert(tk.END, "No files in this group.")
return
for file in files_in_group:
content_listbox.insert(tk.END, file)
except Exception as e:
messagebox.showerror("Error", f"An error occurred while displaying folder contents:\n{str(e)}")
GUI Setup Modifications
Ensure the label positions are set correctly in the grid setup:
python
# Total Files Label next to Parent Folder Name Entry
total_files_label = tk.Label(frame, text="Total Files: 0")
total_files_label.grid(row=0, column=3, padx=5, sticky='w')
# File number info displayed between listboxes and execution
file_number_info = tk.Label(frame, text="", font=('Century Gothic', 8))
file_number_info.grid(row=7, column=0, columnspan=2, pady=(0, 20), sticky='w')
# Browse and Execute Buttons near the bottom edge
browse_button = tk.Button(frame, text="Browse Directory", command=load_directory)
browse_button.grid(row=8, column=0, pady=(10, 0), sticky='w')
execute_button = tk.Button(frame, text="Execute Sorting", command=execute_sorting)
execute_button.grid(row=8, column=3, pady=(10, 0), sticky='e')
These changes should resolve the issues with repeated folder names, accurately reflect file counts in each selected subgroup, and ensure your UI provides informative feedback on the total file count and files per subfolder in the appropriate positions.
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
merge_history = [] # Stack to keep track of merges for undo functionality
rename_in_progress = False # Flag to prevent multiple rename operations
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]
# 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:
return []
# 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)
return redundant_words
def extract_sort_key(filename):
"""
Extract numerical suffix from the filename for sorting.
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:
return {}
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)
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)
return temp_groups
else:
# Treat all files as a single group named "All Files"
return {"All Files": files}
def split_into_subgroups(files, file_num):
"""
Split files into subgroups based on file_num and remaining files rule.
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
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)
return modified_name
def get_preview_groups():
"""
Create a preview of groups by applying pending renames and merges.
Returns:
dict: A dictionary representing the preview groups.
"""
# Start with a copy of the current groups
preview = groups.copy()
# Apply pending renames
for original_group, new_group in pending_renames.items():
if original_group in preview:
preview[new_group] = preview.pop(original_group)
# Apply pending merges
for merge_op in pending_merges:
groups_to_merge = merge_op['groups']
new_group_name = merge_op['new_group']
merged_files = []
for group in groups_to_merge:
merged_files.extend(preview.get(group, []))
if group in preview:
del preview[group]
if new_group_name in preview:
preview[new_group_name].extend(merged_files)
else:
preview[new_group_name] = merged_files
return preview
def update_folder_info(preview_groups):
"""Update the informational labels with folder details."""
total_files_count = sum(len(files) for files in preview_groups.values())
total_files_label.config(text=f"Total Files: {total_files_count}")
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)
file_number_info.config(text="")
return
folder_display_name = folder_listbox.get(selection[0])
files_in_group = list(display_to_group_map.get(folder_display_name, []))
file_number_info.config(text=f"Files in this Subfolder: {len(files_in_group)}")
content_listbox.delete(0, tk.END)
if not files_in_group:
content_listbox.insert(tk.END, "No files in this group.")
return
for file in files_in_group:
content_listbox.insert(tk.END, file)
except Exception as e:
messagebox.showerror("Error", f"An error occurred while displaying folder contents:\n{str(e)}")
def load_directory():
"""Load the selected directory, prepare groups, and update the displayed folders."""
global folder_path, groups, pending_renames, pending_merges, merge_history
folder_path = filedialog.askdirectory(title="Select the folder containing your files")
if not folder_path:
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))]
groups = prepare_groups(files, enable_grouping_var.get())
# Reset any pending operations
pending_renames.clear()
pending_merges.clear()
merge_history.clear()
update_displayed_folders()
except Exception as e:
messagebox.showerror("Error", f"An error occurred while loading the directory:\n{str(e)}")
def apply_options(*args):
"""Apply the selected options and update the preview."""
if not folder_path:
return # Do nothing if no directory is loaded
try:
update_displayed_folders()
except Exception as e:
messagebox.showerror("Error", f"An error occurred while applying options:\n{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))]
groups = prepare_groups(files, enable_grouping_var.get())
update_displayed_folders()
except Exception as e:
messagebox.showerror("Error", f"An error occurred while updating the preview:\n{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()
folder_listbox.delete(0, tk.END)
display_to_group_map.clear()
# Get the preview groups
preview_groups = get_preview_groups()
# Update folder info
update_folder_info(preview_groups)
for folder_name in sorted(preview_groups.keys()):
files = preview_groups.get(folder_name, [])
if not enable_grouping_var.get() and group_files_var.get():
file_num_str = file_number_var.get().strip()
try:
file_num = int(file_num_str) if file_num_str else None
except ValueError:
continue
if file_num and len(files) > 0:
subgroups = split_into_subgroups(files, file_num)
for idx, subgroup in enumerate(subgroups, 1):
subfolder_name = f"{parent_prefix} - {idx}"
folder_listbox.insert(tk.END, subfolder_name)
display_to_group_map[subfolder_name] = tuple(subgroup)
else:
folder_listbox.insert(tk.END, parent_prefix)
display_to_group_map[parent_prefix] = tuple(files)
else:
if capitalize_names:
modified_folder_name = capitalize_folder_name(folder_name)
else:
modified_folder_name = folder_name
if add_prefix:
if modified_folder_name:
modified_folder_name = f"{parent_prefix} - {modified_folder_name}"
else:
modified_folder_name = parent_prefix
folder_listbox.insert(tk.END, modified_folder_name)
display_to_group_map[modified_folder_name] = tuple(files)
if folder_listbox.size() > 0:
folder_listbox.select_set(0)
show_folder_contents()
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.")
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.")
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)
# 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]
else:
# Create new group
merged_files = []
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]
# Push to merge history for undo functionality
merge_history.append(merge_operation)
# Clear pending merges after applying
pending_merges.clear()
# Now proceed with file moving based on updated groups
for folder_display_name, group_name in display_to_group_map.items():
if isinstance(group_name, tuple):
files = list(group_name)
else:
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}")
continue
os.makedirs(new_folder_path, exist_ok=True)
if group_files and file_num and len(files) > 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)
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
else:
# Move files directly to the group folder if no subgrouping is needed
for file in files:
src = os.path.join(folder_path, file)
dest = os.path.join(new_folder_path, file)
shutil.move(src, dest)
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 and len(files) > 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}")
continue
os.makedirs(subfolder_path, exist_ok=True)
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
else:
# If grouping is disabled and all files fit or no subgrouping is needed, move directly into a single appropriate folder
for file in files:
src = os.path.join(folder_path, file)
dest = os.path.join(folder_path, file) # Ensure this is correct relative move
shutil.move(src, dest)
else:
# Edge Case: Grouping enabled but only "All Files" group exists
# Treat it as grouping disabled
if group_files and file_num and len(files) > 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)
os.makedirs(subfolder_path, exist_ok=True)
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
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 subfolder with the same name as the parent folder
if os.path.abspath(subfolder_path) == os.path.abspath(folder_path):
pass
else:
os.makedirs(subfolder_path, exist_ok=True)
for file in files:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
messagebox.showinfo("Success", "Files sorted and organized successfully!")
load_directory() # Reload to reflect changes without opening the folder
except Exception as e:
messagebox.showerror("Error", f"An error occurred during sorting:\n{str(e)}")
# ---------------------------- New Functions ----------------------------
def rename_selected_folder():
"""Rename the selected folder directly in the listbox."""
global rename_in_progress
selected_indices = folder_listbox.curselection()
if len(selected_indices) != 1:
messagebox.showinfo("Rename Folder", "Please select exactly one folder to rename.")
return
if rename_in_progress:
messagebox.showinfo("Rename in Progress", "Please complete the current rename operation before starting a new one.")
return
rename_in_progress = True
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)
# 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])
def on_enter(event=None):
global rename_in_progress
new_display_name = entry.get().strip()
if not new_display_name:
messagebox.showerror("Invalid Name", "Folder name cannot be empty.")
rename_in_progress = False
entry.destroy()
root.unbind("<Button-1>")
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.")
rename_in_progress = False
entry.destroy()
root.unbind("<Button-1>")
return
# Register the pending rename
pending_renames[original_group_name] = new_display_name
# 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]
entry.destroy()
rename_in_progress = False
# Unbind the <Button-1> event
root.unbind("<Button-1>")
# Update the preview to reflect the rename
update_displayed_folders()
# 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.")
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]
# 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)
if not new_folder_name:
messagebox.showerror("Invalid Name", "Folder name cannot be empty.")
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.")
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.")
return
# Register the pending merge
pending_merges.append({'groups': selected_group_names, 'new_group': new_folder_name})
# Push to merge history for undo functionality
merge_history.append({'groups': selected_group_names, 'new_group': new_folder_name})
# 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]
# 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
# Update the preview to reflect the merge
update_displayed_folders()
def undo_merge():
"""Undo the last merge operation."""
if not merge_history:
messagebox.showinfo("Undo Merge", "No merge operations to undo.")
return
last_merge = merge_history.pop()
groups_to_split = last_merge['groups']
new_group_name = last_merge['new_group']
# Remove the merged group from pending_merges
pending_merges[:] = [merge for merge in pending_merges if merge['new_group'] != new_group_name]
# Re-add the original groups to pending merges
for group in groups_to_split:
# Find the original folder name before merge
original_group_name = group # Assuming group names are original
# Re-add as individual groups
pending_renames.pop(original_group_name, None) # Remove any pending renames on these groups if necessary
# Update the listbox to reflect the undone merge
folder_listbox.delete(tk.END) # Remove the merged folder
for group in groups_to_split:
folder_listbox.insert(tk.END, group)
display_to_group_map[group] = group
# Remove the merged folder display mapping
if new_group_name in display_to_group_map:
del display_to_group_map[new_group_name]
# Update the preview to reflect the undone merge
update_displayed_folders()
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)
# 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)
# Add Undo Merge option if there are merges to undo
if merge_history:
context_menu.add_separator()
context_menu.add_command(label="Undo Last Merge", command=undo_merge)
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)}")
# ---------------------------- GUI Setup ----------------------------
# Updated GUI Setup
root = tk.Tk()
root.title("File Sorter")
root.geometry("900x600") # Set the window size
root.resizable(False, False) # Make window non-resizable
frame = tk.Frame(root)
frame.pack(pady=20, padx=20, fill=tk.BOTH, expand=True)
# Adjusted widths: you can tweak these numbers to better position the visual divider
folder_listbox_width = 40
content_listbox_width = 70
# Parent Folder Name
parent_name_label = tk.Label(frame, text="Parent Folder Name:")
parent_name_label.grid(row=0, column=0, padx=5, sticky='w')
parent_name_var = tk.StringVar()
parent_name_entry = tk.Entry(frame, width=50, textvariable=parent_name_var)
parent_name_entry.grid(row=0, column=1, columnspan=2, padx=5, sticky='ew')
parent_name_var.trace_add('write', lambda *args: apply_options())
# Total Files Label next to Parent Folder Name Entry
total_files_label = tk.Label(frame, text="Total Files: 0")
total_files_label.grid(row=0, column=3, padx=5, sticky='w')
# Checkboxes aligned to the left edge of the Folder Listbox
prefix_var = tk.IntVar(value=0) # Checkbox unchecked by default
prefix_check = tk.Checkbutton(frame, text="Add Parent Name Prefix", variable=prefix_var, command=apply_options)
prefix_check.grid(row=1, column=0, columnspan=2, pady=5, sticky='w')
group_files_var = tk.IntVar(value=1) # Checkbox checked by default
group_files_check = tk.Checkbutton(frame, text="Group by file number", variable=group_files_var, command=apply_options)
group_files_check.grid(row=2, column=0, pady=5, sticky='w')
# File number entry aligned after Group by file number
file_number_label = tk.Label(frame, text="Files per subfolder:")
file_number_label.grid(row=2, column=1, padx=0, sticky='w')
file_number_var = tk.StringVar(value="30") # Default for file grouping
file_number_entry = tk.Entry(frame, width=5, textvariable=file_number_var)
file_number_entry.grid(row=2, column=1, sticky='w', padx=(110, 0))
file_number_var.trace_add('write', lambda *args: apply_options())
capitalize_var = tk.IntVar(value=1) # Checkbox checked by default
capitalize_check = tk.Checkbutton(frame, text="Capitalize Folder Names", variable=capitalize_var, command=apply_options)
capitalize_check.grid(row=3, column=0, columnspan=2, pady=5, sticky='w')
# Folder Listbox (Preview) with Enable Grouping
enable_grouping_var = tk.IntVar(value=1) # Checkbox checked by default
enable_grouping_check = tk.Checkbutton(frame, text="Enable Folder Grouping", variable=enable_grouping_var, command=apply_options)
enable_grouping_check.grid(row=4, column=0, columnspan=2, pady=5, sticky='w')
# Folder Listbox Header
folder_listbox_label = tk.Label(frame, text="Grouped Folders (Preview):", font=('Century Gothic', 10, 'bold'))
folder_listbox_label.grid(row=5, column=0, sticky='w')
# Folder Listbox
folder_listbox = tk.Listbox(frame, width=folder_listbox_width, height=15, exportselection=False, selectmode=tk.EXTENDED)
folder_listbox.grid(row=6, column=0, columnspan=2, pady=5, sticky='nsew')
folder_listbox.bind("<<ListboxSelect>>", show_folder_contents)
folder_listbox.bind("<Double-Button-1>", lambda event: rename_selected_folder()) # Double-click to rename
# Bind right-click for context menu
folder_listbox.bind("<Button-3>", create_context_menu) # Right-click on Windows
folder_listbox.bind("<Button-2>", create_context_menu) # Right-click on macOS
# Content Listbox Header
content_listbox_label = tk.Label(frame, text="Folder Contents (Preview):", font=('Century Gothic', 10, 'bold'))
content_listbox_label.grid(row=5, column=2, sticky='w')
# Content Listbox
content_listbox = tk.Listbox(frame, width=content_listbox_width, height=15, exportselection=False)
content_listbox.grid(row=6, column=2, columnspan=2, pady=5, sticky='nsew')
# Make the listboxes expand with window resizing
frame.grid_rowconfigure(6, weight=1)
frame.grid_columnconfigure(1, weight=1)
# File number info displayed between listboxes and execution
file_number_info = tk.Label(frame, text="", font=('Century Gothic', 8))
file_number_info.grid(row=7, column=0, columnspan=2, pady=(0, 20), sticky='w')
# Browse and Execute Buttons near the bottom edge
browse_button = tk.Button(frame, text="Browse Directory", command=load_directory)
browse_button.grid(row=8, column=0, pady=(10, 0), sticky='w')
execute_button = tk.Button(frame, text="Execute Sorting", command=execute_sorting)
execute_button.grid(row=8, column=3, pady=(10, 0), sticky='e')
root.mainloop()
Key Improvements:
Correct Subfolder Indexing: The script properly increments index values for subgroup folders, addressing the repeated subfolder names.
Reinstating Total Files Information: The total_files_label has been re-added and correctly updated to display the total number of files.
Show Files per Subfolder: This label is positioned between the filename listbox and the execute button as per your preference.
These adjustments should correctly solve both the repeated subfolder name problem and improve the file count information display.
code
# 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)
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:
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)
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)
return temp_groups
else:
# Treat all files as a single group named "All Files"
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
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)
return modified_name
def get_preview_groups():
"""
Create a preview of groups by applying pending renames and merges.
code
Returns:
dict: A dictionary representing the preview groups.
"""
# Start with a copy of the current groups
preview = groups.copy()
# Apply pending renames
for original_group, new_group in pending_renames.items():
if original_group in preview:
preview[new_group] = preview.pop(original_group)
# Apply pending merges
for merge_op in pending_merges:
groups_to_merge = merge_op['groups']
new_group_name = merge_op['new_group']
merged_files = []
for group in groups_to_merge:
merged_files.extend(preview.get(group, []))
if group in preview:
del preview[group]
if new_group_name in preview:
preview[new_group_name].extend(merged_files)
else:
preview[new_group_name] = merged_files
return preview
def update_folder_info(preview_groups):
"""Update the informational labels with folder details."""
# Clear previous info
total_files_label.config(text=f"Total Files: {sum(len(files) for files in preview_groups.values())}")
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)
return
code
folder_display_name = folder_listbox.get(selection[0])
original_group_name = display_to_group_map.get(folder_display_name, folder_display_name)
# Get the preview groups
preview_groups = get_preview_groups()
# Update folder info
update_folder_info(preview_groups)
# Clear previous contents
content_listbox.delete(0, tk.END)
# Get the list of files in the selected group, considering pending operations
files_in_group = preview_groups.get(original_group_name, [])
if not files_in_group:
content_listbox.insert(tk.END, "No files in this group.")
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
return
# Calculate subgroups based on the file_num
subgroups = split_into_subgroups(files_in_group, 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 files_in_group:
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
return
# Calculate subgroups based on the file_num
subgroups = split_into_subgroups(files_in_group, 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 files_in_group:
content_listbox.insert(tk.END, file)
else:
for file in files_in_group:
content_listbox.insert(tk.END, file)
# Display file number info for Grouped Folders
if enable_grouping_var.get():
file_number_info.config(text=f"Files in this Group: {len(files_in_group)}")
else:
file_number_info.config(text="")
except Exception as e:
messagebox.showerror("Error", f"An error occurred while displaying folder contents:\n{str(e)}")
def load_directory():
"""Load the selected directory, prepare groups, and update the displayed folders."""
global folder_path, groups, pending_renames, pending_merges, merge_history
folder_path = filedialog.askdirectory(title="Select the folder containing your files")
if not folder_path:
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))]
groups = prepare_groups(files, enable_grouping_var.get())
# Reset any pending operations
pending_renames.clear()
pending_merges.clear()
merge_history.clear()
update_displayed_folders()
except Exception as e:
messagebox.showerror("Error", f"An error occurred while loading the directory:\n{str(e)}")
def apply_options(*args):
"""Apply the selected options and update the preview."""
if not folder_path:
return # Do nothing if no directory is loaded
try:
update_displayed_folders()
except Exception as e:
messagebox.showerror("Error", f"An error occurred while applying options:\n{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))]
groups = prepare_groups(files, enable_grouping_var.get())
update_displayed_folders()
except Exception as e:
messagebox.showerror("Error", f"An error occurred while updating the preview:\n{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()
# Get the preview groups
preview_groups = get_preview_groups()
# Update folder info
update_folder_info(preview_groups)
for folder_name in sorted(preview_groups.keys()):
# When grouping is disabled and "Group by file number" is enabled,
# create virtual subgroups for display
if not enable_grouping_var.get() and group_files_var.get():
file_num_str = file_number_var.get().strip()
try:
file_num = int(file_num_str) if file_num_str else None
except ValueError:
continue
files = preview_groups.get(folder_name, [])
if file_num:
subgroups = split_into_subgroups(files, file_num)
for idx, subgroup in enumerate(subgroups, 1):
subfolder_name = f"{parent_prefix} - {idx}"
folder_listbox.insert(tk.END, subfolder_name)
display_to_group_map[subfolder_name] = (folder_name, idx, subgroup) # Map uses a tuple for subgroups
else:
folder_listbox.insert(tk.END, parent_prefix)
display_to_group_map[parent_prefix] = folder_name
else:
# Regular processing for enabled grouping
display_name = folder_name
if capitalize_names:
modified_folder_name = capitalize_folder_name(display_name)
else:
modified_folder_name = display_name
if add_prefix:
if modified_folder_name:
modified_folder_name = f"{parent_prefix} - {modified_folder_name}"
else:
modified_folder_name = parent_prefix
folder_listbox.insert(tk.END, modified_folder_name)
display_to_group_map[modified_folder_name] = folder_name
# Automatically select the first folder and show its contents
if folder_listbox.size() > 0:
folder_listbox.select_set(0)
show_folder_contents()
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)
return
code
folder_display_name = folder_listbox.get(selection[0])
mapping = display_to_group_map.get(folder_display_name, folder_display_name)
# Get the preview groups
preview_groups = get_preview_groups()
# Update folder info
update_folder_info(preview_groups)
# Clear previous contents
content_listbox.delete(0, tk.END)
if isinstance(mapping, tuple):
# When using file number grouping with group_files_var enabled and enable_grouping_var disabled
files_in_group = mapping[2] # This is the subgroup from the mapping tuple
else:
# For regular groups
files_in_group = preview_groups.get(mapping, [])
if not files_in_group:
content_listbox.insert(tk.END, "No files in this group.")
return
for file in files_in_group:
content_listbox.insert(tk.END, file)
# Display file number info if grouping is supported
if enable_grouping_var.get():
file_number_info.config(text=f"Files in this Group: {len(files_in_group)}")
else:
file_number_info.config(text="")
except Exception as e:
messagebox.showerror("Error", f"An error occurred while displaying folder contents:\n{str(e)}")
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.")
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.")
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)
# 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]
else:
# Create new group
merged_files = []
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]
# Push to merge history for undo functionality
merge_history.append(merge_operation)
# Clear pending merges after applying
pending_merges.clear()
# Now proceed with file moving based on updated groups
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}")
continue
os.makedirs(new_folder_path, exist_ok=True)
if group_files and file_num and len(files) > 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)
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
else:
# Move files directly to the group folder if no subgrouping is needed
for file in files:
src = os.path.join(folder_path, file)
dest = os.path.join(new_folder_path, file)
shutil.move(src, dest)
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 and len(files) > 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}")
continue
os.makedirs(subfolder_path, exist_ok=True)
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
else:
# If no subgrouping is needed, move directly into a single appropriate folder
for file in files:
src = os.path.join(folder_path, file)
dest = os.path.join(folder_path, file)
shutil.move(src, dest)
else:
# Edge Case: Grouping enabled but only "All Files" group exists
# Treat it as grouping disabled
if group_files and file_num and len(files) > 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)
os.makedirs(subfolder_path, exist_ok=True)
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
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 subfolder with the same name as the parent folder
if os.path.abspath(subfolder_path) == os.path.abspath(folder_path):
pass
else:
os.makedirs(subfolder_path, exist_ok=True)
for file in files:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
messagebox.showinfo("Success", "Files sorted and organized successfully!")
load_directory() # Reload to reflect changes without opening the folder
except Exception as e:
messagebox.showerror("Error", f"An error occurred during sorting:\n{str(e)}")
---------------------------- New Functions ----------------------------
def rename_selected_folder():
"""Rename the selected folder directly in the listbox."""
global rename_in_progress
selected_indices = folder_listbox.curselection()
if len(selected_indices) != 1:
messagebox.showinfo("Rename Folder", "Please select exactly one folder to rename.")
return
code
if rename_in_progress:
messagebox.showinfo("Rename in Progress", "Please complete the current rename operation before starting a new one.")
return
rename_in_progress = True
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)
# 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])
def on_enter(event=None):
global rename_in_progress
new_display_name = entry.get().strip()
if not new_display_name:
messagebox.showerror("Invalid Name", "Folder name cannot be empty.")
rename_in_progress = False
entry.destroy()
root.unbind("<Button-1>")
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.")
rename_in_progress = False
entry.destroy()
root.unbind("<Button-1>")
return
# Register the pending rename
pending_renames[original_group_name] = new_display_name
# 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]
entry.destroy()
rename_in_progress = False
# Unbind the <Button-1> event
root.unbind("<Button-1>")
# Update the preview to reflect the rename
update_displayed_folders()
# 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.")
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]
# 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)
if not new_folder_name:
messagebox.showerror("Invalid Name", "Folder name cannot be empty.")
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.")
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.")
return
# Register the pending merge
pending_merges.append({'groups': selected_group_names, 'new_group': new_folder_name})
# Push to merge history for undo functionality
merge_history.append({'groups': selected_group_names, 'new_group': new_folder_name})
# 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]
# 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
# Update the preview to reflect the merge
update_displayed_folders()
def undo_merge():
"""Undo the last merge operation."""
if not merge_history:
messagebox.showinfo("Undo Merge", "No merge operations to undo.")
return
code
last_merge = merge_history.pop()
groups_to_split = last_merge['groups']
new_group_name = last_merge['new_group']
# Remove the merged group from pending_merges
pending_merges[:] = [merge for merge in pending_merges if merge['new_group'] != new_group_name]
# Re-add the original groups to pending merges
for group in groups_to_split:
# Find the original folder name before merge
original_group_name = group # Assuming group names are original
# Re-add as individual groups
pending_renames.pop(original_group_name, None) # Remove any pending renames on these groups if necessary
# Update the listbox to reflect the undone merge
folder_listbox.delete(tk.END) # Remove the merged folder
for group in groups_to_split:
folder_listbox.insert(tk.END, group)
display_to_group_map[group] = group
# Remove the merged folder display mapping
if new_group_name in display_to_group_map:
del display_to_group_map[new_group_name]
# Update the preview to reflect the undone merge
update_displayed_folders()
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)
# Add Undo Merge option if there are merges to undo
if merge_history:
context_menu.add_separator()
context_menu.add_command(label="Undo Last Merge", command=undo_merge)
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)}")
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}
merge_history = [] # Stack to keep track of merges for undo functionality
pending_merges = [] # Tracks pending merges
rename_in_progress = False # Flag to prevent multiple rename operations
def clean_filename(filename, redundant_words):
"""Clean filename according to specified rules (remove numbers, repeated words)."""
# Ignore text before and including ':'.
filename = filename.split(':', 1)[-1]
# Remove file extension.
filename = os.path.splitext(filename)[0]
# 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)
# 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()
# Remove redundant words/groups of characters.
unique_words = [w for w in words if w not in redundant_words]
# 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:
return []
# 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)
return redundant_words
def extract_sort_key(filename):
"""Extract numerical suffix from the filename for sorting."""
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:
return {}
if grouping_enabled:
redundant_words = find_redundant_words(files)
temp_groups = defaultdict(list)
for file in files:
cleaned_name = clean_filename(file, redundant_words)
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)
return temp_groups
else:
# Treat all files as a single group named "All Files"
return {"All Files": files}
def split_into_subgroups(files, file_num):
"""Split files into subgroups based on file_num and remaining files rule."""
subgroups = []
total_files = len(files)
index = 0
while index < total_files:
remaining = total_files - index
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
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)
return modified_name
def get_preview_groups():
"""Create a preview of groups by applying pending renames and merges."""
# Start with a copy of the current groups
preview = groups.copy()
# Apply pending renames
for original_group, new_group in pending_renames.items():
if original_group in preview:
preview[new_group] = preview.pop(original_group)
# Apply pending merges
for merge_op in pending_merges:
groups_to_merge = merge_op['groups']
new_group_name = merge_op['new_group']
merged_files = []
for group in groups_to_merge:
merged_files.extend(preview.get(group, []))
if group in preview:
del preview[group]
if new_group_name in preview:
preview[new_group_name].extend(merged_files)
else:
preview[new_group_name] = merged_files
return preview
def update_folder_info(preview_groups):
"""Update the informational labels with folder details."""
total_files_count = sum(len(files) for files in preview_groups.values())
total_files_label.config(text=f"Total Files: {total_files_count}")
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)
file_number_info.config(text="")
return
folder_display_name = folder_listbox.get(selection[0])
files_in_group = list(display_to_group_map.get(folder_display_name, []))
file_number_info.config(text=f"Files in this Subfolder: {len(files_in_group)}")
content_listbox.delete(0, tk.END)
if not files_in_group:
content_listbox.insert(tk.END, "No files in this group.")
return
for file in files_in_group:
content_listbox.insert(tk.END, file)
except Exception as e:
messagebox.showerror("Error", f"An error occurred while displaying folder contents:\n{str(e)}")
def load_directory():
"""Load the selected directory, prepare groups, and update the displayed folders."""
global folder_path, groups, pending_renames, pending_merges, merge_history
folder_path = filedialog.askdirectory(title="Select the folder containing your files")
if not folder_path:
return
parent_folder_name = os.path.basename(folder_path)
parent_name_var.set(parent_folder_name)
try:
files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
groups = prepare_groups(files, enable_grouping_var.get())
pending_renames.clear()
pending_merges.clear()
merge_history.clear()
update_displayed_folders()
except Exception as e:
messagebox.showerror("Error", f"An error occurred while loading the directory:\n{str(e)}")
def apply_options(*args):
"""Apply the selected options and update the preview."""
if not folder_path:
return
try:
update_displayed_folders()
except Exception as e:
messagebox.showerror("Error", f"An error occurred while applying options:\n{str(e)}")
def load_directory_preview():
"""Load the directory and update the display based on the current settings without moving files."""
global groups
try:
files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
groups = prepare_groups(files, enable_grouping_var.get())
update_displayed_folders()
except Exception as e:
messagebox.showerror("Error", f"An error occurred while updating the preview:\n{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()
folder_listbox.delete(0, tk.END)
display_to_group_map.clear()
preview_groups = get_preview_groups()
update_folder_info(preview_groups)
for folder_name in sorted(preview_groups.keys()):
files = preview_groups.get(folder_name, [])
if not enable_grouping_var.get() and group_files_var.get():
file_num_str = file_number_var.get().strip()
try:
file_num = int(file_num_str) if file_num_str else None
except ValueError:
continue
if file_num and len(files) > 0:
subgroups = split_into_subgroups(files, file_num)
for idx, subgroup in enumerate(subgroups, 1):
subfolder_name = f"{parent_prefix} - {idx}"
folder_listbox.insert(tk.END, subfolder_name)
display_to_group_map[subfolder_name] = tuple(subgroup)
else:
folder_listbox.insert(tk.END, parent_prefix)
display_to_group_map[parent_prefix] = tuple(files)
else:
if capitalize_names:
modified_folder_name = capitalize_folder_name(folder_name)
else:
modified_folder_name = folder_name
if add_prefix:
if modified_folder_name:
modified_folder_name = f"{parent_prefix} - {modified_folder_name}"
else:
modified_folder_name = parent_prefix
folder_listbox.insert(tk.END, modified_folder_name)
display_to_group_map[modified_folder_name] = tuple(files)
if folder_listbox.size() > 0:
folder_listbox.select_set(0)
show_folder_contents()
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.")
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()
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.")
return
else:
file_num = None
for original_group, new_group in pending_renames.items():
if original_group not in groups:
continue
groups[new_group] = groups.pop(original_group)
pending_renames.clear()
for merge_operation in pending_merges:
group_names = merge_operation['groups']
new_group_name = merge_operation['new_group']
if new_group_name in groups:
merged_files = groups[new_group_name]
else:
merged_files = []
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]
merge_history.append(merge_operation)
pending_merges.clear()
for folder_display_name, group_name in display_to_group_map.items():
if isinstance(group_name, tuple):
files = list(group_name)
else:
files = groups.get(group_name, [])
if grouping_enabled and group_name != "All Files":
if capitalize_names:
base_folder_name = capitalize_folder_name(group_name)
else:
base_folder_name = group_name
if add_prefix:
if base_folder_name:
modified_folder_name = f"{parent_prefix} - {base_folder_name}"
else:
modified_folder_name = parent_prefix
else:
modified_folder_name = base_folder_name
new_folder_path = os.path.join(folder_path, modified_folder_name)
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}")
continue
os.makedirs(new_folder_path, exist_ok=True)
if group_files and file_num and len(files) > 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)
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
else:
for file in files:
src = os.path.join(folder_path, file)
dest = os.path.join(new_folder_path, file)
shutil.move(src, dest)
elif not grouping_enabled and group_name == "All Files":
if group_files and file_num and len(files) > 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)
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}")
continue
os.makedirs(subfolder_path, exist_ok=True)
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
else:
for file in files:
src = os.path.join(folder_path, file)
dest = os.path.join(folder_path, file)
shutil.move(src, dest)
else:
if group_files and file_num and len(files) > 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)
os.makedirs(subfolder_path, exist_ok=True)
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
elif not group_files:
subfolder_name = f"{parent_prefix}"
subfolder_path = os.path.join(folder_path, subfolder_name)
if os.path.abspath(subfolder_path) == os.path.abspath(folder_path):
pass
else:
os.makedirs(subfolder_path, exist_ok=True)
for file in files:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
messagebox.showinfo("Success", "Files sorted and organized successfully!")
load_directory()
except Exception as e:
messagebox.showerror("Error", f"An error occurred during sorting:\n{str(e)}")
def rename_selected_folder():
"""Rename the selected folder directly in the listbox."""
global rename_in_progress
selected_indices = folder_listbox.curselection()
if len(selected_indices) != 1:
messagebox.showinfo("Rename Folder", "Please select exactly one folder to rename.")
return
if rename_in_progress:
messagebox.showinfo("Rename in Progress", "Please complete the current rename operation before starting a new one.")
return
rename_in_progress = True
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)
entry = tk.Entry(frame, width=40)
entry.insert(0, old_display_name)
entry.select_range(0, tk.END)
entry.focus()
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])
def on_enter(event=None):
global rename_in_progress
new_display_name = entry.get().strip()
if not new_display_name:
messagebox.showerror("Invalid Name", "Folder name cannot be empty.")
rename_in_progress = False
entry.destroy()
root.unbind("<Button-1>")
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.")
rename_in_progress = False
entry.destroy()
root.unbind("<Button-1>")
return
pending_renames[original_group_name] = new_display_name
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]
entry.destroy()
rename_in_progress = False
root.unbind("<Button-1>")
update_displayed_folders()
entry.bind("<Return>", on_enter)
def on_click_anywhere(event):
widget = event.widget
if widget != entry:
on_enter()
root.bind("<Button-1>", on_click_anywhere)
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.")
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]
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)
if not new_folder_name:
messagebox.showerror("Invalid Name", "Folder name cannot be empty.")
return
# Ensure we handle the case of an existing merged folder name within the pending merges
if any(new_folder_name in merge['new_group'] for merge in pending_merges):
messagebox.showerror("Duplicate Name", "A pending merge to this name already exists.")
return
# Register the pending merge and also ensure it's unique
pending_merges.append({'groups': selected_group_names, 'new_group': new_folder_name})
# Remove existing folders from display to simulate the merge
for i in reversed(selected_indices):
display_name = folder_listbox.get(i)
folder_listbox.delete(i)
del display_to_group_map[display_name]
# Add the new merged folder to the listbox
folder_listbox.insert(tk.END, new_folder_name)
display_to_group_map[new_folder_name] = new_folder_name
# Push merge operation to history for undo capability
merge_history.append({'groups': selected_group_names, 'new_group': new_folder_name})
def undo_merge():
"""Undo the last merge operation."""
if not merge_history:
messagebox.showinfo("Undo Merge", "No merge operations to undo.")
return
last_merge = merge_history.pop()
groups_to_split = last_merge['groups']
new_group_name = last_merge['new_group']
pending_merges[:] = [merge for merge in pending_merges if merge['new_group'] != new_group_name]
for group in groups_to_split:
folder_listbox.insert(tk.END, group)
display_to_group_map[group] = group
if new_group_name in display_to_group_map:
del display_to_group_map[new_group_name]
update_displayed_folders()
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)
selected_indices = folder_listbox.curselection()
if len(selected_indices) > 1:
context_menu.add_command(label="Merge Selected Folders", command=merge_selected_folders)
if merge_history:
context_menu.add_separator()
context_menu.add_command(label="Undo Last Merge", command=undo_merge)
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)}")
# GUI Setup
root = tk.Tk()
root.title("File Sorter")
root.geometry("900x600")
root.resizable(False, False)
frame = tk.Frame(root)
frame.pack(pady=20, padx=20, fill=tk.BOTH, expand=True)
folder_listbox_width = 40
content_listbox_width = 70
parent_name_label = tk.Label(frame, text="Parent Folder Name:")
parent_name_label.grid(row=0, column=0, padx=5, sticky='w')
parent_name_var = tk.StringVar()
parent_name_entry = tk.Entry(frame, width=50, textvariable=parent_name_var)
parent_name_entry.grid(row=0, column=1, columnspan=2, padx=5, sticky='ew')
parent_name_var.trace_add('write', lambda *args: apply_options())
total_files_label = tk.Label(frame, text="Total Files: 0")
total_files_label.grid(row=0, column=3, padx=5, sticky='w')
prefix_var = tk.IntVar(value=0)
prefix_check = tk.Checkbutton(frame, text="Add Parent Name Prefix", variable=prefix_var, command=apply_options)
prefix_check.grid(row=1, column=0, columnspan=2, pady=5, sticky='w')
group_files_var = tk.IntVar(value=1)
group_files_check = tk.Checkbutton(frame, text="Group by file number", variable=group_files_var, command=apply_options)
group_files_check.grid(row=2, column=0, pady=5, sticky='w')
file_number_label = tk.Label(frame, text="Files per subfolder:")
file_number_label.grid(row=2, column=1, padx=0, sticky='w')
file_number_var = tk.StringVar(value="30")
file_number_entry = tk.Entry(frame, width=5, textvariable=file_number_var)
file_number_entry.grid(row=2, column=1, sticky='w', padx=(110, 0))
file_number_var.trace_add('write', lambda *args: apply_options())
capitalize_var = tk.IntVar(value=1)
capitalize_check = tk.Checkbutton(frame, text="Capitalize Folder Names", variable=capitalize_var, command=apply_options)
capitalize_check.grid(row=3, column=0, columnspan=2, pady=5, sticky='w')
enable_grouping_var = tk.IntVar(value=1)
enable_grouping_check = tk.Checkbutton(frame, text="Enable Folder Grouping", variable=enable_grouping_var, command=apply_options)
enable_grouping_check.grid(row=4, column=0, columnspan=2, pady=5, sticky='w')
folder_listbox_label = tk.Label(frame, text="Grouped Folders (Preview):", font=('Century Gothic', 10, 'bold'))
folder_listbox_label.grid(row=5, column=0, sticky='w')
folder_listbox = tk.Listbox(frame, width=folder_listbox_width, height=15, exportselection=False, selectmode=tk.EXTENDED)
folder_listbox.grid(row=6, column=0, columnspan=2, pady=5, sticky='nsew')
folder_listbox.bind("<<ListboxSelect>>", show_folder_contents)
folder_listbox.bind("<Double-Button-1>", lambda event: rename_selected_folder())
folder_listbox.bind("<Button-3>", create_context_menu)
folder_listbox.bind("<Button-2>", create_context_menu)
content_listbox_label = tk.Label(frame, text="Folder Contents (Preview):", font=('Century Gothic', 10, 'bold'))
content_listbox_label.grid(row=5, column=2, sticky='w')
content_listbox = tk.Listbox(frame, width=content_listbox_width, height=15, exportselection=False)
content_listbox.grid(row=6, column=2, columnspan=2, pady=5, sticky='nsew')
frame.grid_rowconfigure(6, weight=1)
frame.grid_columnconfigure(1, weight=1)
file_number_info = tk.Label(frame, text="", font=('Century Gothic', 8))
file_number_info.grid(row=7, column=0, columnspan=2, pady=(0, 20), sticky='w')
browse_button = tk.Button(frame, text="Browse Directory", command=load_directory)
browse_button.grid(row=8, column=0, pady=(10, 0), sticky='w')
execute_button = tk.Button(frame, text="Execute Sorting", command=execute_sorting)
execute_button.grid(row=8, column=3, pady=(10, 0), sticky='e')
root.mainloop()
Key Changes and Improvements:
Merge Functionality: The merging logic is improved to check for duplicate names among pending merges and make sure new groups don't conflict.
Display Adjustments: Folder names with or without prefix and capitalization properly integrate user options.
Consistent File Handling: Address range and list handling for file moves ensures folders update correctly without creating unnecessary folders or duplicates.
These steps will help maintain integrity within the application's file sorting and merging processes.
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}
merge_history = [] # Stack to keep track of merges for undo functionality
pending_merges = [] # Tracks pending merges
rename_in_progress = False # Flag to prevent multiple rename operations
def clean_filename(filename, redundant_words):
"""Clean filename according to specified rules (remove numbers, repeated words)."""
# Ignore text before and including ':'.
filename = filename.split(':', 1)[-1]
# Remove file extension.
filename = os.path.splitext(filename)[0]
# 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)
# 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()
# Remove redundant words/groups of characters.
unique_words = [w for w in words if w not in redundant_words]
# 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:
return []
# 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)
return redundant_words
def extract_sort_key(filename):
"""Extract numerical suffix from the filename for sorting."""
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:
return {}
if grouping_enabled:
redundant_words = find_redundant_words(files)
temp_groups = defaultdict(list)
for file in files:
cleaned_name = clean_filename(file, redundant_words)
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)
return temp_groups
else:
# Treat all files as a single group named "All Files"
return {"All Files": files}
def split_into_subgroups(files, file_num):
"""Split files into subgroups based on file_num and remaining files rule."""
subgroups = []
total_files = len(files)
index = 0
while index < total_files:
remaining = total_files - index
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
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)
return modified_name
def get_preview_groups():
"""Create a preview of groups by applying pending renames and merges."""
# Start with a copy of the current groups
preview = groups.copy()
# Apply pending renames
for original_group, new_group in pending_renames.items():
if original_group in preview:
preview[new_group] = preview.pop(original_group)
# Apply pending merges
for merge_op in pending_merges:
groups_to_merge = merge_op['groups']
new_group_name = merge_op['new_group']
merged_files = []
for group in groups_to_merge:
merged_files.extend(preview.get(group, []))
if group in preview:
del preview[group]
if new_group_name in preview:
preview[new_group_name].extend(merged_files)
else:
preview[new_group_name] = merged_files
return preview
def update_folder_info(preview_groups):
"""Update the informational labels with folder details."""
total_files_count = sum(len(files) for files in preview_groups.values())
total_files_label.config(text=f"Total Files: {total_files_count}")
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)
file_number_info.config(text="")
return
folder_display_name = folder_listbox.get(selection[0])
files_in_group = list(display_to_group_map.get(folder_display_name, []))
file_number_info.config(text=f"Files in this Subfolder: {len(files_in_group)}")
content_listbox.delete(0, tk.END)
if not files_in_group:
content_listbox.insert(tk.END, "No files in this group.")
return
for file in files_in_group:
content_listbox.insert(tk.END, file)
except Exception as e:
messagebox.showerror("Error", f"An error occurred while displaying folder contents:\n{str(e)}")
def load_directory():
"""Load the selected directory, prepare groups, and update the displayed folders."""
global folder_path, groups, pending_renames, pending_merges, merge_history
folder_path = filedialog.askdirectory(title="Select the folder containing your files")
if not folder_path:
return
parent_folder_name = os.path.basename(folder_path)
parent_name_var.set(parent_folder_name)
try:
files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
groups = prepare_groups(files, enable_grouping_var.get())
pending_renames.clear()
pending_merges.clear()
merge_history.clear()
update_displayed_folders()
except Exception as e:
messagebox.showerror("Error", f"An error occurred while loading the directory:\n{str(e)}")
def apply_options(*args):
"""Apply the selected options and update the preview."""
if not folder_path:
return
try:
update_displayed_folders()
except Exception as e:
messagebox.showerror("Error", f"An error occurred while applying options:\n{str(e)}")
def load_directory_preview():
"""Load the directory and update the display based on the current settings without moving files."""
global groups
try:
files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
groups = prepare_groups(files, enable_grouping_var.get())
update_displayed_folders()
except Exception as e:
messagebox.showerror("Error", f"An error occurred while updating the preview:\n{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()
folder_listbox.delete(0, tk.END)
display_to_group_map.clear()
preview_groups = get_preview_groups()
update_folder_info(preview_groups)
for folder_name in sorted(preview_groups.keys()):
files = preview_groups.get(folder_name, [])
if not enable_grouping_var.get() and group_files_var.get():
file_num_str = file_number_var.get().strip()
try:
file_num = int(file_num_str) if file_num_str else None
except ValueError:
continue
if file_num and len(files) > 0:
subgroups = split_into_subgroups(files, file_num)
for idx, subgroup in enumerate(subgroups, 1):
subfolder_name = f"{parent_prefix} - {idx}"
folder_listbox.insert(tk.END, subfolder_name)
display_to_group_map[subfolder_name] = tuple(subgroup)
else:
folder_listbox.insert(tk.END, parent_prefix)
display_to_group_map[parent_prefix] = tuple(files)
else:
if capitalize_names:
modified_folder_name = capitalize_folder_name(folder_name)
else:
modified_folder_name = folder_name
if add_prefix:
if modified_folder_name:
modified_folder_name = f"{parent_prefix} - {modified_folder_name}"
else:
modified_folder_name = parent_prefix
folder_listbox.insert(tk.END, modified_folder_name)
display_to_group_map[modified_folder_name] = tuple(files)
if folder_listbox.size() > 0:
folder_listbox.select_set(0)
show_folder_contents()
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.")
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()
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.")
return
else:
file_num = None
for original_group, new_group in pending_renames.items():
if original_group not in groups:
continue
groups[new_group] = groups.pop(original_group)
pending_renames.clear()
for merge_operation in pending_merges:
group_names = merge_operation['groups']
new_group_name = merge_operation['new_group']
if new_group_name in groups:
merged_files = groups[new_group_name]
else:
merged_files = []
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]
merge_history.append(merge_operation)
pending_merges.clear()
for folder_display_name, group_name in display_to_group_map.items():
if isinstance(group_name, tuple):
files = list(group_name)
else:
files = groups.get(group_name, [])
if grouping_enabled and group_name != "All Files":
if capitalize_names:
base_folder_name = capitalize_folder_name(group_name)
else:
base_folder_name = group_name
if add_prefix:
if base_folder_name:
modified_folder_name = f"{parent_prefix} - {base_folder_name}"
else:
modified_folder_name = parent_prefix
else:
modified_folder_name = base_folder_name
new_folder_path = os.path.join(folder_path, modified_folder_name)
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}")
continue
os.makedirs(new_folder_path, exist_ok=True)
if group_files and file_num and len(files) > 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)
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
else:
for file in files:
src = os.path.join(folder_path, file)
dest = os.path.join(new_folder_path, file)
shutil.move(src, dest)
elif not grouping_enabled and group_name == "All Files":
if group_files and file_num and len(files) > 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)
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}")
continue
os.makedirs(subfolder_path, exist_ok=True)
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
else:
for file in files:
src = os.path.join(folder_path, file)
dest = os.path.join(folder_path, file)
shutil.move(src, dest)
else:
if group_files and file_num and len(files) > 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)
os.makedirs(subfolder_path, exist_ok=True)
for file in subgroup:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
elif not group_files:
subfolder_name = f"{parent_prefix}"
subfolder_path = os.path.join(folder_path, subfolder_name)
if os.path.abspath(subfolder_path) == os.path.abspath(folder_path):
pass
else:
os.makedirs(subfolder_path, exist_ok=True)
for file in files:
src = os.path.join(folder_path, file)
dest = os.path.join(subfolder_path, file)
shutil.move(src, dest)
messagebox.showinfo("Success", "Files sorted and organized successfully!")
load_directory()
except Exception as e:
messagebox.showerror("Error", f"An error occurred during sorting:\n{str(e)}")
def rename_selected_folder():
"""Rename the selected folder directly in the listbox."""
global rename_in_progress
selected_indices = folder_listbox.curselection()
if len(selected_indices) != 1:
messagebox.showinfo("Rename Folder", "Please select exactly one folder to rename.")
return
if rename_in_progress:
messagebox.showinfo("Rename in Progress", "Please complete the current rename operation before starting a new one.")
return
rename_in_progress = True
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)
entry = tk.Entry(frame, width=40)
entry.insert(0, old_display_name)
entry.select_range(0, tk.END)
entry.focus()
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])
def on_enter(event=None):
global rename_in_progress
new_display_name = entry.get().strip()
if not new_display_name:
messagebox.showerror("Invalid Name", "Folder name cannot be empty.")
rename_in_progress = False
entry.destroy()
root.unbind("<Button-1>")
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.")
rename_in_progress = False
entry.destroy()
root.unbind("<Button-1>")
return
pending_renames[original_group_name] = new_display_name
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]
entry.destroy()
rename_in_progress = False
root.unbind("<Button-1>")
update_displayed_folders()
entry.bind("<Return>", on_enter)
def on_click_anywhere(event):
widget = event.widget
if widget != entry:
on_enter()
root.bind("<Button-1>", on_click_anywhere)
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.")
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]
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)
if not new_folder_name:
messagebox.showerror("Invalid Name", "Folder name cannot be empty.")
return
# Ensure we handle the case of an existing merged folder name within the pending merges
if any(new_folder_name in merge['new_group'] for merge in pending_merges):
messagebox.showerror("Duplicate Name", "A pending merge to this name already exists.")
return
# Register the pending merge and also ensure it's unique
pending_merges.append({'groups': selected_group_names, 'new_group': new_folder_name})
# Remove existing folders from display to simulate the merge
for i in reversed(selected_indices):
display_name = folder_listbox.get(i)
folder_listbox.delete(i)
del display_to_group_map[display_name]
# Add the new merged folder to the listbox
folder_listbox.insert(tk.END, new_folder_name)
display_to_group_map[new_folder_name] = list(itertools.chain.from_iterable(groups[name] for name in selected_group_names if name in groups)) # Correctly merge file lists
# Push merge operation to history for undo capability
merge_history.append({'groups': selected_group_names, 'new_group': new_folder_name})
def undo_merge():
"""Undo the last merge operation."""
if not merge_history:
messagebox.showinfo("Undo Merge", "No merge operations to undo.")
return
last_merge = merge_history.pop()
groups_to_split = last_merge['groups']
new_group_name = last_merge['new_group']
pending_merges[:] = [merge for merge in pending_merges if merge['new_group'] != new_group_name]
for group in groups_to_split:
folder_listbox.insert(tk.END, group)
display_to_group_map[group] = group
if new_group_name in display_to_group_map:
del display_to_group_map[new_group_name]
update_displayed_folders()
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)
selected_indices = folder_listbox.curselection()
if len(selected_indices) > 1:
context_menu.add_command(label="Merge Selected Folders", command=merge_selected_folders)
if merge_history:
context_menu.add_separator()
context_menu.add_command(label="Undo Last Merge", command=undo_merge)
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)}")
# GUI Setup
root = tk.Tk()
root.title("File Sorter")
root.geometry("900x600")
root.resizable(False, False)
frame = tk.Frame(root)
frame.pack(pady=20, padx=20, fill=tk.BOTH, expand=True)
folder_listbox_width = 40
content_listbox_width = 70
parent_name_label = tk.Label(frame, text="Parent Folder Name:")
parent_name_label.grid(row=0, column=0, padx=5, sticky='w')
parent_name_var = tk.StringVar()
parent_name_entry = tk.Entry(frame, width=50, textvariable=parent_name_var)
parent_name_entry.grid(row=0, column=1, columnspan=2, padx=5, sticky='ew')
parent_name_var.trace_add('write', lambda *args: apply_options())
total_files_label = tk.Label(frame, text="Total Files: 0")
total_files_label.grid(row=0, column=3, padx=5, sticky='w')
prefix_var = tk.IntVar(value=0)
prefix_check = tk.Checkbutton(frame, text="Add Parent Name Prefix", variable=prefix_var, command=apply_options)
prefix_check.grid(row=1, column=0, columnspan=2, pady=5, sticky='w')
group_files_var = tk.IntVar(value=1)
group_files_check = tk.Checkbutton(frame, text="Group by file number", variable=group_files_var, command=apply_options)
group_files_check.grid(row=2, column=0, pady=5, sticky='w')
file_number_label = tk.Label(frame, text="Files per subfolder:")
file_number_label.grid(row=2, column=1, padx=0, sticky='w')
file_number_var = tk.StringVar(value="30")
file_number_entry = tk.Entry(frame, width=5, textvariable=file_number_var)
file_number_entry.grid(row=2, column=1, sticky='w', padx=(110, 0))
file_number_var.trace_add('write', lambda *args: apply_options())
capitalize_var = tk.IntVar(value=1)
capitalize_check = tk.Checkbutton(frame, text="Capitalize Folder Names", variable=capitalize_var, command=apply_options)
capitalize_check.grid(row=3, column=0, columnspan=2, pady=5, sticky='w')
enable_grouping_var = tk.IntVar(value=1)
enable_grouping_check = tk.Checkbutton(frame, text="Enable Folder Grouping", variable=enable_grouping_var, command=apply_options)
enable_grouping_check.grid(row=4, column=0, columnspan=2, pady=5, sticky='w')
folder_listbox_label = tk.Label(frame, text="Grouped Folders (Preview):", font=('Century Gothic', 10, 'bold'))
folder_listbox_label.grid(row=5, column=0, sticky='w')
folder_listbox = tk.Listbox(frame, width=folder_listbox_width, height=15, exportselection=False, selectmode=tk.EXTENDED)
folder_listbox.grid(row=6, column=0, columnspan=2, pady=5, sticky='nsew')
folder_listbox.bind("<<ListboxSelect>>", show_folder_contents)
folder_listbox.bind("<Double-Button-1>", lambda event: rename_selected_folder())
folder_listbox.bind("<Button-3>", create_context_menu)
folder_listbox.bind("<Button-2>", create_context_menu)
content_listbox_label = tk.Label(frame, text="Folder Contents (Preview):", font=('Century Gothic', 10, 'bold'))
content_listbox_label.grid(row=5, column=2, sticky='w')
content_listbox = tk.Listbox(frame, width=content_listbox_width, height=15, exportselection=False)
content_listbox.grid(row=6, column=2, columnspan=2, pady=5, sticky='nsew')
frame.grid_rowconfigure(6, weight=1)
frame.grid_columnconfigure(1, weight=1)
file_number_info = tk.Label(frame, text="", font=('Century Gothic', 8))
file_number_info.grid(row=7, column=0, columnspan=2, pady=(0, 20), sticky='w')
browse_button = tk.Button(frame, text="Browse Directory", command=load_directory)
browse_button.grid(row=8, column=0, pady=(10, 0), sticky='w')
execute_button = tk.Button(frame, text="Execute Sorting", command=execute_sorting)
execute_button.grid(row=8, column=3, pady=(10, 0), sticky='e')
root.mainloop()
Key Changes and Improvements:
Correct Handling of Merges: Ensure that the merged content isn't just the name being split into characters. Utilize lists to correctly group file names together and update display_to_group_map with actual file lists.
Consistent UI Behavior: Smooth integration of changes so your application should now handle folder merges as expected without unusual display issues.