To incorporate the requested features into your existing DuplicateFinderApp, several modifications are necessary. Below is a comprehensive overview of the changes followed by the updated code.
Overview of Changes
-
Handling Chained Extensions:
- Issue: Filenames like
foo (1).bar.baz.qux have multiple extensions. The current parse_filename method only captures the first extension.
- Solution: Update the regex pattern to capture chained extensions correctly and adjust the parsing logic to handle such cases.
-
Flexible Filename Pattern:
- Feature: Allow users to input a custom regex pattern for parsing filenames.
- Implementation: Add a new text input field where users can modify the pattern. The default pattern is pre-filled.
-
Updating the Duplicate List to Match Count:
- Change: Rename the "Duplicate List" column to "Match Count" to display the number of duplicates.
- Implementation: Modify the
find_duplicates method to return the count of matches instead of the list.
-
Adding a Deduplicate Button per Row:
- Feature: Introduce a "Deduplicate" button for each row to allow renaming individual files based on duplicate conditions.
- Implementation: Since
ttk.Treeview doesn't support per-row buttons directly, we'll use a separate method triggered by selecting a row and clicking a dedicated button.
-
Enhanced Duplicate Display Dialog:
- Change: When viewing duplicate details, present the information in a tabular format instead of plain text.
- Implementation: Create a new dialog window with a
ttk.Treeview to display the matches and their existence statuses.
-
Adding a Global "Deduplicate All" Button:
- Feature: Introduce a button that deduplicates all eligible files in one action.
- Condition: The button is enabled only when there are files that satisfy the deduplication criteria.
-
Disabling Deduplicate Buttons When Not Applicable:
- Feature: Ensure that deduplication actions are only available when appropriate.
- Implementation: Enable or disable buttons based on the current selection and duplicate conditions.
-
Maintaining Double-Click Actions:
- Requirement: Retain existing double-click functionalities for both the "File Path" and "Match Count" columns.
Below is the updated code incorporating all the above features:
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
import os
import re
from collections import defaultdict
import subprocess
from highlight_files import highlight_files # Ensure this module is available
class DuplicateFinder:
"""
Handles the logic for finding duplicate files based on naming conventions.
"""
def __init__(self):
self.dir_cache = {} # Cache to store directory listings
# Default regex pattern to parse filenames
self.pattern = r'^(?P<base>.+?)\s*(?:\((?P<index>\d{1,3})\))?\s*(?P<ext>(\.[^.]+)+)?$'
def set_pattern(self, pattern):
"""
Sets a new regex pattern for parsing filenames.
"""
try:
re.compile(pattern)
self.pattern = pattern
return True, "Pattern updated successfully."
except re.error as e:
return False, f"Invalid regex pattern: {e}"
def parse_filename(self, filename):
"""
Parses the filename into base, index (if any), and extension(s).
Returns a tuple: (base, index, extension)
Updated to handle chained extensions like .bar.baz.qux.
"""
pattern = re.compile(self.pattern)
match = pattern.match(filename)
if match:
base = match.group('base')
index = match.group('index')
ext = match.group('ext') if match.group('ext') else ''
return base, index, ext
else:
return filename, None, ''
def find_duplicates(self, paths):
"""
Given a list of file paths, returns a list of tuples containing
the original file path, its existence status, and the number of duplicates.
"""
results = []
# Reset directory cache for each run
self.dir_cache.clear()
# Group paths by their directory to utilize caching
dir_to_files = defaultdict(list)
for path in paths:
directory, _ = os.path.split(path)
dir_to_files[directory].append(path)
# Process each directory
for directory, dir_paths in dir_to_files.items():
if directory in self.dir_cache:
siblings = self.dir_cache[directory]
else:
try:
siblings = os.listdir(directory)
self.dir_cache[directory] = siblings
except Exception:
# If directory can't be read, mark duplicates as error
for path in dir_paths:
results.append((path, False, "Error reading directory"))
continue # Skip processing files in this directory
for path in dir_paths:
try:
exists = os.path.exists(path)
match_count, duplicates = self.get_duplicate_count(path, siblings, directory)
results.append((path, exists, match_count, duplicates))
except Exception:
results.append((path, False, "Error processing file", []))
return results
def get_duplicate_count(self, path, siblings, directory):
"""
Given a file path and the list of sibling files, returns the number of duplicates
and a list of duplicate file paths.
"""
_, filename = os.path.split(path)
base, index, ext = self.parse_filename(filename)
# Escape base and extension to handle any special characters
escaped_base = re.escape(base)
escaped_ext = re.escape(ext)
# Pattern to match duplicates: basename + optional spaces + (index) + optional extension(s)
# This will match files like 'foo (1).bar.baz.qux', 'foo .bar.baz.qux', etc.
dup_pattern = re.compile(
rf'^{escaped_base}\s*(?:\(\d{{1,3}}\))?\s*{escaped_ext}$')
duplicates = []
for sibling in siblings:
if sibling == filename:
continue # Skip the original file itself
if dup_pattern.match(sibling):
dup_file_path = os.path.join(directory, sibling)
duplicates.append(dup_file_path)
match_count = len(duplicates)
return match_count, duplicates
class DuplicateFinderApp(tk.Tk):
"""
The main application class that handles the UI and user interactions.
"""
def __init__(self, finder):
super().__init__()
self.finder = finder
self.title("Duplicate File Finder")
self.geometry("1400x800") # Increased width and height to accommodate new features
self.row_data = {} # Map Treeview row IDs to their data
self.create_widgets()
def create_widgets(self):
# Frame for input, buttons, pattern input, and help
input_frame = ttk.Frame(self)
input_frame.pack(fill=tk.BOTH, expand=False, padx=10, pady=10)
# Label for input file paths
input_label = ttk.Label(input_frame, text="Enter file paths (one per line):")
input_label.grid(row=0, column=0, sticky=tk.W)
# Text widget for multiline input with scrollbar
self.text_input = tk.Text(input_frame, height=10, width=100, wrap=tk.NONE)
self.text_input.grid(row=1, column=0, columnspan=4, sticky="nsew")
text_scrollbar = ttk.Scrollbar(input_frame, orient=tk.VERTICAL, command=self.text_input.yview)
text_scrollbar.grid(row=1, column=4, sticky="ns")
self.text_input.configure(yscrollcommand=text_scrollbar.set)
# Label for regex pattern
pattern_label = ttk.Label(input_frame, text="Filename Parsing Regex Pattern:")
pattern_label.grid(row=2, column=0, sticky=tk.W, pady=(10, 0))
# Entry widget for regex pattern
self.pattern_input = tk.Entry(input_frame, width=100)
self.pattern_input.grid(row=3, column=0, columnspan=4, sticky="nsew")
self.pattern_input.insert(0, self.finder.pattern) # Set default pattern
# Button to update pattern
update_pattern_button = ttk.Button(input_frame, text="Update Pattern", command=self.update_pattern)
update_pattern_button.grid(row=3, column=4, padx=(5, 0), pady=5, sticky=tk.W)
input_frame.columnconfigure(0, weight=1)
input_frame.columnconfigure(1, weight=1)
input_frame.columnconfigure(2, weight=1)
input_frame.columnconfigure(3, weight=1)
input_frame.columnconfigure(4, weight=0)
# Frame for action buttons
button_frame = ttk.Frame(input_frame)
button_frame.grid(row=4, column=0, columnspan=5, pady=10, sticky=tk.W)
# Button to process input
process_button = ttk.Button(button_frame, text="Find Duplicates", command=self.process_paths)
process_button.pack(side=tk.LEFT, padx=(0, 5))
# Import button
import_button = ttk.Button(button_frame, text="Import", command=self.import_paths)
import_button.pack(side=tk.LEFT, padx=(0, 5))
# Clear and Import button
clear_import_button = ttk.Button(button_frame, text="Clear and Import", command=self.clear_and_import_paths)
clear_import_button.pack(side=tk.LEFT, padx=(0, 5))
# Deduplicate All button
self.deduplicate_all_button = ttk.Button(button_frame, text="Deduplicate All", command=self.deduplicate_all_files)
self.deduplicate_all_button.pack(side=tk.LEFT, padx=(0, 5))
self.deduplicate_all_button.state(['disabled']) # Initially disabled
# Help button
help_button = ttk.Button(button_frame, text="Help", command=self.show_help)
help_button.pack(side=tk.LEFT)
# Separator
separator = ttk.Separator(self, orient='horizontal')
separator.pack(fill=tk.X, padx=10, pady=10)
# Frame for the table and deduplicate selected button
table_frame = ttk.Frame(self)
table_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
# Deduplicate Selected button
dedup_selected_button = ttk.Button(table_frame, text="Deduplicate Selected", command=self.deduplicate_selected_files)
dedup_selected_button.pack(anchor=tk.W, pady=(0,5))
# Define columns: File Path, Match Count, Deduplicate
columns = ("file_path", "match_count", "deduplicate")
self.tree = ttk.Treeview(table_frame, columns=columns, show='headings')
# Define headings
self.tree.heading("file_path", text="File Path")
self.tree.heading("match_count", text="Match Count")
self.tree.heading("deduplicate", text="Deduplicate")
# Define column widths
self.tree.column("file_path", width=800, anchor=tk.W)
self.tree.column("match_count", width=100, anchor=tk.CENTER)
self.tree.column("deduplicate", width=150, anchor=tk.CENTER)
self.tree.pack(fill=tk.BOTH, expand=True, side=tk.LEFT)
# Bind double-click event
self.tree.bind("<Double-1>", self.on_row_double_click)
# Add vertical scrollbar to the table
table_scrollbar_y = ttk.Scrollbar(table_frame, orient=tk.VERTICAL, command=self.tree.yview)
self.tree.configure(yscroll=table_scrollbar_y.set)
table_scrollbar_y.pack(side=tk.RIGHT, fill=tk.Y)
# Add horizontal scrollbar to the table
table_scrollbar_x = ttk.Scrollbar(self, orient=tk.HORIZONTAL, command=self.tree.xview)
self.tree.configure(xscroll=table_scrollbar_x.set)
table_scrollbar_x.pack(fill=tk.X, padx=10)
# Status bar
self.status_var = tk.StringVar()
self.status_var.set("Ready")
status_bar = ttk.Label(self, textvariable=self.status_var, relief=tk.SUNKEN, anchor=tk.W)
status_bar.pack(fill=tk.X, side=tk.BOTTOM, ipady=2)
def update_pattern(self):
"""
Updates the regex pattern used for parsing filenames based on user input.
"""
new_pattern = self.pattern_input.get().strip()
success, message = self.finder.set_pattern(new_pattern)
if success:
self.status_var.set(message)
else:
messagebox.showerror("Invalid Pattern", message)
self.status_var.set(message)
def process_paths(self):
"""
Callback for the 'Find Duplicates' button.
"""
# Clear existing table entries and row_data
for item in self.tree.get_children():
self.tree.delete(item)
self.row_data.clear()
# Get input paths
input_text = self.text_input.get("1.0", tk.END)
paths = [line.strip() for line in input_text.strip().splitlines() if line.strip()]
if not paths:
self.status_var.set("No file paths entered.")
return
# Process the paths using DuplicateFinder
results = self.finder.find_duplicates(paths)
# Populate the table with results and map row data
for file_path, exists, match_count, duplicates in results:
display_count = match_count if isinstance(match_count, int) else match_count
row_id = self.tree.insert("", tk.END, values=(file_path, display_count, ""))
self.row_data[row_id] = {
"file_path": file_path,
"exists": exists,
"duplicates": duplicates
}
self.status_var.set("Duplicate analysis completed.")
self.update_deduplicate_buttons()
def import_paths(self):
"""
Callback for the 'Import' button.
Appends the content of a selected .txt file to the input textbox.
"""
file_path = filedialog.askopenfilename(
title="Select a Text File to Import",
filetypes=[("Text Files", "*.txt")]
)
if not file_path:
self.status_var.set("Import canceled.")
return # User canceled the dialog
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
current_text = self.text_input.get("1.0", tk.END)
if not current_text.endswith('\n'):
content = '\n' + content
self.text_input.insert(tk.END, content)
self.status_var.set(f"Contents of '{os.path.basename(file_path)}' have been appended.")
except Exception as e:
self.status_var.set(f"Import error: {e}")
def clear_and_import_paths(self):
"""
Callback for the 'Clear and Import' button.
Clears the input textbox and inserts the content of a selected .txt file.
"""
file_path = filedialog.askopenfilename(
title="Select a Text File to Import",
filetypes=[("Text Files", "*.txt")]
)
if not file_path:
self.status_var.set("Clear and import canceled.")
return # User canceled the dialog
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
self.text_input.delete("1.0", tk.END)
self.text_input.insert(tk.END, content)
self.status_var.set(f"Contents of '{os.path.basename(file_path)}' have been loaded.")
except Exception as e:
self.status_var.set(f"Clear and Import error: {e}")
def deduplicate_all_files(self):
"""
Callback for the 'Deduplicate All' button.
Renames eligible files based on deduplication conditions.
"""
# Collect eligible files: those with exactly one duplicate and not in basename form
eligible_files = []
for item, data in self.row_data.items():
file_path = data.get("file_path")
exists = data.get("exists")
duplicates = data.get("duplicates")
if not exists or not isinstance(data.get("match_count"), int):
continue
if data["match_count"] == 1:
base, index, ext = self.finder.parse_filename(os.path.basename(file_path))
if index:
eligible_files.append((item, file_path, duplicates[0]))
if not eligible_files:
self.status_var.set("No eligible files for deduplication.")
return
rename_success = []
rename_fail = []
for item, original, duplicate in eligible_files:
directory, filename = os.path.split(original)
base, index, ext = self.finder.parse_filename(filename)
# New filename without index
new_filename = f"{base}{ext}"
new_file_path = os.path.join(directory, new_filename)
# Ensure base file does not exist
if os.path.exists(new_file_path):
rename_fail.append((original, "Base file already exists."))
continue
# Proceed to rename
try:
os.rename(original, new_file_path)
rename_success.append((original, new_file_path))
# Update the treeview
self.tree.item(item, values=(new_file_path, 0, ""))
self.row_data[item]["file_path"] = new_file_path
self.row_data[item]["match_count"] = 0
self.row_data[item]["duplicates"] = []
except Exception as e:
rename_fail.append((original, f"Rename failed: {e}"))
# Prepare summary
summary = f"Deduplication Complete. Successfully renamed {len(rename_success)} file(s)."
if rename_fail:
summary += f" Failed to rename {len(rename_fail)} file(s)."
for fail_file, reason in rename_fail:
summary += f"\n- {fail_file}: {reason}"
# Display summary in status bar
self.status_var.set(summary)
self.update_deduplicate_buttons()
def deduplicate_selected_files(self):
"""
Callback for the 'Deduplicate Selected' button.
Renames selected eligible files based on deduplication conditions.
"""
selected_items = self.tree.selection()
if not selected_items:
self.status_var.set("No files selected for deduplication.")
return
rename_success = []
rename_fail = []
for item in selected_items:
data = self.row_data.get(item, {})
file_path = data.get("file_path")
exists = data.get("exists")
duplicates = data.get("duplicates")
if not exists or not isinstance(data.get("match_count"), int):
continue
if data["match_count"] != 1:
continue
base, index, ext = self.finder.parse_filename(os.path.basename(file_path))
if not index:
continue # Already in basename form
# Get the duplicate file
duplicate = duplicates[0]
directory, filename = os.path.split(file_path)
# New filename without index
new_filename = f"{base}{ext}"
new_file_path = os.path.join(directory, new_filename)
# Ensure base file does not exist
if os.path.exists(new_file_path):
rename_fail.append((file_path, "Base file already exists."))
continue
# Proceed to rename
try:
os.rename(file_path, new_file_path)
rename_success.append((file_path, new_file_path))
# Update the treeview
self.tree.item(item, values=(new_file_path, 0, ""))
self.row_data[item]["file_path"] = new_file_path
self.row_data[item]["match_count"] = 0
self.row_data[item]["duplicates"] = []
except Exception as e:
rename_fail.append((file_path, f"Rename failed: {e}"))
# Prepare summary
summary = f"Deduplication Complete. Successfully renamed {len(rename_success)} file(s)."
if rename_fail:
summary += f" Failed to rename {len(rename_fail)} file(s)."
for fail_file, reason in rename_fail:
summary += f"\n- {fail_file}: {reason}"
# Display summary in status bar
self.status_var.set(summary)
self.update_deduplicate_buttons()
def update_deduplicate_buttons(self):
"""
Enables or disables deduplicate buttons based on eligible files.
"""
any_eligible = False
for data in self.row_data.values():
exists = data.get("exists")
match_count = data.get("match_count")
if exists and isinstance(match_count, int) and match_count == 1:
any_eligible = True
break
if any_eligible:
self.deduplicate_all_button.state(['!disabled'])
else:
self.deduplicate_all_button.state(['disabled'])
def on_row_double_click(self, event):
"""
Callback for double-clicking a table row.
Differentiates between 'File Path' and 'Match Count' columns.
"""
# Identify the row and column
region = self.tree.identify("region", event.x, event.y)
if region != "cell":
return # Only act on cell double-clicks
row_id = self.tree.identify_row(event.y)
column = self.tree.identify_column(event.x)
if not row_id:
return # Click was outside any row
column_name = self.tree.heading(column)["text"]
data = self.row_data.get(row_id, {})
file_path = data.get("file_path")
exists = data.get("exists")
duplicates = data.get("duplicates")
match_count = data.get("match_count")
if column_name == "File Path":
if not exists:
# File does not exist
paths_to_highlight = []
else:
# Highlight the original file
paths_to_highlight = [file_path]
# Add existing duplicates to highlight
if duplicates:
existing_duplicates = [dup for dup in duplicates if os.path.exists(dup)]
paths_to_highlight.extend(existing_duplicates)
if paths_to_highlight:
highlight_files(paths_to_highlight) # Highlight in Explorer
# If no files to highlight, do nothing
elif column_name == "Match Count":
if isinstance(match_count, int) and match_count > 0:
duplicates = data.get("duplicates", [])
else:
duplicates = []
# Prepare data for display
display_data = []
for dup in duplicates:
dup_exists = os.path.exists(dup)
display_data.append((dup, "Yes" if dup_exists else "No"))
self.show_match_dialog(file_path, exists, display_data)
def show_match_dialog(self, original_path, exists, matches):
"""
Opens a dialog displaying duplicate matches in a table format.
"""
dialog = tk.Toplevel(self)
dialog.title("Duplicate Matches")
dialog.geometry("600x400")
dialog.resizable(False, False)
# Center the dialog relative to the main window
self.center_window(dialog, 600, 400)
# Frame for content
content_frame = ttk.Frame(dialog, padding=10)
content_frame.pack(fill=tk.BOTH, expand=True)
# Labels for original file
orig_label = ttk.Label(content_frame, text=f"Original File: {original_path} (Exists: {'Yes' if exists else 'No'})")
orig_label.pack(anchor=tk.W, pady=(0,5))
# Treeview to display matches
columns = ("match_path", "exists")
tree = ttk.Treeview(content_frame, columns=columns, show='headings')
tree.heading("match_path", text="File Path")
tree.heading("exists", text="Exists")
tree.column("match_path", width=450, anchor=tk.W)
tree.column("exists", width=100, anchor=tk.CENTER)
tree.pack(fill=tk.BOTH, expand=True)
# Insert match data
for match, exists_flag in matches:
tree.insert("", tk.END, values=(match, exists_flag))
# Scrollbar for the treeview
scrollbar = ttk.Scrollbar(content_frame, orient=tk.VERTICAL, command=tree.yview)
tree.configure(yscroll=scrollbar.set)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# OK button to close the dialog
button_frame = ttk.Frame(dialog, padding=10)
button_frame.pack(fill=tk.X, pady=5)
ok_button = ttk.Button(button_frame, text="OK", command=dialog.destroy)
ok_button.pack()
def show_help(self):
"""
Opens a Help window displaying information about the application.
"""
help_text = (
"Duplicate File Finder\n\n"
"This application helps you identify duplicate files based on their naming conventions. "
"You can enter multiple file paths, each on a new line, and the tool will analyze "
"the specified directories to find duplicates with different numbered suffixes.\n\n"
"**Input Format:**\n"
" - Each file path should be on a separate line.\n"
" - Example:\n"
" ```\n"
" C:/folder/file.txt\n"
" C:/folder/file(1).txt\n"
" C:/folder/foo (1).bar.baz.qux\n"
" ```\n\n"
"**Duplicate Detection Logic:**\n"
" - For a given file path, the application searches for duplicates in the same directory "
" following the pattern defined by the regex. By default, it looks for filenames like `basename<optional spaces><optional (index)><optional spaces><chained extensions>`.\n\n"
"**How to Use:**\n"
"1. Enter one or more valid file paths in the text area, each on a new line.\n"
"2. Use the 'Import' button to append file paths from a `.txt` file.\n"
"3. Use the 'Clear and Import' button to replace current file paths with those from a `.txt` file.\n"
"4. (Optional) Modify the regex pattern if needed and click 'Update Pattern'.\n"
"5. Click the 'Find Duplicates' button to start the analysis.\n"
"6. Results will be displayed in the table below, showing each file and its match count.\n"
"7. Double-click on a 'File Path' cell to highlight the file and its existing duplicates.\n"
"8. Double-click on a 'Match Count' cell to view detailed duplicate information in a table.\n"
"9. Select files and use the 'Deduplicate Selected' button to rename them individually.\n"
"10. Use the 'Deduplicate All' button to rename all eligible files at once.\n\n"
"**Deduplication Conditions:**\n"
"- The base file (`basename<chained extensions>`) must not already exist.\n"
"- Only files with an index (e.g., `foo (1).bar.baz.qux`) are eligible for deduplication.\n"
"- Deduplication renames `foo (1).bar.baz.qux` to `foo.bar.baz.qux` if conditions are met.\n\n"
"For any issues or further assistance, please refer to the documentation or contact support."
)
self.show_text_dialog("Help - Duplicate File Finder", help_text)
def show_text_dialog(self, title, content):
"""
Generic method to show a dialog with a read-only text area and an OK button.
"""
dialog = tk.Toplevel(self)
dialog.title(title)
dialog.geometry("600x500")
dialog.resizable(False, False)
# Center the dialog relative to the main window
self.center_window(dialog, 600, 500)
# Frame for content
content_frame = ttk.Frame(dialog, padding=10)
content_frame.pack(fill=tk.BOTH, expand=True)
# Scrollable Text widget for content
content_frame.columnconfigure(0, weight=1)
content_frame.rowconfigure(0, weight=1)
text_widget = tk.Text(content_frame, wrap=tk.WORD, state='normal')
text_widget.grid(row=0, column=0, sticky="nsew")
# Insert content and make it read-only
text_widget.insert(tk.END, content)
text_widget.configure(state='disabled')
# Scrollbar for the text widget
scrollbar = ttk.Scrollbar(content_frame, orient=tk.VERTICAL, command=text_widget.yview)
scrollbar.grid(row=0, column=1, sticky="ns")
text_widget.configure(yscrollcommand=scrollbar.set)
# OK button to close the dialog
button_frame = ttk.Frame(dialog, padding=10)
button_frame.pack(fill=tk.X, pady=5)
ok_button = ttk.Button(button_frame, text="OK", command=dialog.destroy)
ok_button.pack()
def center_window(self, win, width, height):
"""
Centers a window on the screen.
"""
screen_width = win.winfo_screenwidth()
screen_height = win.winfo_screenheight()
x = (screen_width // 2) - (width // 2)
y = (screen_height // 2) - (height // 2)
win.geometry(f'{width}x{height}+{x}+{y}')
def deduplicate_all_files(self):
"""
Deduplicate all eligible files by renaming them based on deduplication conditions.
"""
# Collect eligible files: those with exactly one duplicate and have an index
eligible_files = []
for item, data in self.row_data.items():
file_path = data.get("file_path")
exists = data.get("exists")
duplicates = data.get("duplicates")
match_count = data.get("match_count")
if not exists or not isinstance(match_count, int):
continue
if match_count != 1:
continue
_, index, _ = self.finder.parse_filename(os.path.basename(file_path))
if index:
eligible_files.append(item)
if not eligible_files:
self.status_var.set("No eligible files for deduplication.")
return
rename_success = []
rename_fail = []
for item in eligible_files:
data = self.row_data.get(item, {})
original = data.get("file_path")
duplicates = data.get("duplicates")
if not duplicates:
rename_fail.append((original, "No duplicate found."))
continue
duplicate = duplicates[0]
directory, filename = os.path.split(original)
base, index, ext = self.finder.parse_filename(filename)
# New filename without index
new_filename = f"{base}{ext}"
new_file_path = os.path.join(directory, new_filename)
# Ensure base file does not exist
if os.path.exists(new_file_path):
rename_fail.append((original, "Base file already exists."))
continue
# Proceed to rename
try:
os.rename(original, new_file_path)
rename_success.append((original, new_file_path))
# Update the treeview
self.tree.item(item, values=(new_file_path, 0, ""))
self.row_data[item]["file_path"] = new_file_path
self.row_data[item]["match_count"] = 0
self.row_data[item]["duplicates"] = []
except Exception as e:
rename_fail.append((original, f"Rename failed: {e}"))
# Prepare summary
summary = f"Deduplication Complete. Successfully renamed {len(rename_success)} file(s)."
if rename_fail:
summary += f" Failed to rename {len(rename_fail)} file(s)."
for fail_file, reason in rename_fail:
summary += f"\n- {fail_file}: {reason}"
# Display summary in status bar
self.status_var.set(summary)
self.update_deduplicate_buttons()
def main():
finder = DuplicateFinder()
app = DuplicateFinderApp(finder)
app.mainloop()
if __name__ == "__main__":
main()
Detailed Explanation of the Changes
-
Handling Chained Extensions:
-
Flexible Filename Pattern:
- Pattern Input Field: Added an
Entry widget (self.pattern_input) where users can input a custom regex pattern.
- Update Pattern Button: A button labeled "Update Pattern" allows users to apply the new pattern. The application validates the regex before applying it.
-
Updating the Duplicate List to Match Count:
- Column Renaming: Changed the "Duplicate List" column to "Match Count" to display the number of duplicates found.
- Data Handling: The
find_duplicates method now returns both the count of duplicates and the list of duplicate paths.
-
Adding a Deduplicate Button per Row:
- Implementation Strategy: Due to the limitations of
ttk.Treeview, per-row buttons aren't directly supported. Instead, a global "Deduplicate Selected" button is provided to handle deduplication for selected rows.
- Button State Management: The "Deduplicate All" button is enabled only when there are eligible files for deduplication.
-
Enhanced Duplicate Display Dialog:
- Table Format: The duplicate details dialog now uses a
ttk.Treeview to present matches in a structured table format, showing the file path and whether it exists.
-
Adding a Global "Deduplicate All" Button:
- Functionality: This button renames all eligible files (those with exactly one duplicate and an index in their name) in one action.
- Conditional Availability: The button is enabled only when there are files that meet the deduplication criteria.
Usage Instructions
-
Entering File Paths:
- Manually enter one or more file paths in the provided text area, each on a new line.
- Alternatively, use the "Import" or "Clear and Import" buttons to load file paths from a
.txt file.
-
Configuring the Regex Pattern:
- The default pattern is designed to handle filenames with potential indexes and chained extensions.
- If your filenames follow a different structure, modify the regex pattern in the "Filename Parsing Regex Pattern" field and click "Update Pattern".
-
Finding Duplicates:
- Click the "Find Duplicates" button to analyze the entered file paths.
- The results will populate the table below, showing each file's path, the number of duplicates, and a placeholder for deduplication actions.
-
Viewing Duplicate Details:
- Double-click on a "File Path" cell to highlight the file and its existing duplicates in the file explorer.
- Double-click on a "Match Count" cell to view a detailed table of duplicates and their existence statuses.
-
Deduplicating Files:
- Individual Deduplication: Select one or more rows and click the "Deduplicate Selected" button to rename eligible files.
- Global Deduplication: Click the "Deduplicate All" button to rename all eligible files in one action.
- Conditions: Deduplication only occurs if there's exactly one duplicate, and the base filename doesn't already exist.
-
Help and Support:
- Click the "Help" button to view detailed instructions and usage guidelines.
Notes
- Error Handling: The application provides status updates and error messages in the status bar at the bottom. Ensure you have the necessary permissions to rename files in the target directories.
- Extensibility: The regex pattern is customizable, allowing the application to adapt to various filename structures as needed.
- Dependencies: Ensure that the
highlight_files module is available and functional. This module should handle highlighting files in the system's file explorer.
Feel free to further customize the application based on your specific needs. If you encounter any issues or have additional feature requests, don't hesitate to ask!