Files
personal_development/servermanagerbase/stashtoolkit/Transcoded File Scripts/Find duplicates/findanddelete.py
T
2026-01-03 13:30:35 -05:00

71 lines
2.6 KiB
Python

import os
import csv
import shutil
from collections import defaultdict
def find_duplicate_basenames(root_directory, valid_extensions):
# Dictionary to store basenames and their corresponding file paths
basenames_to_paths = defaultdict(list)
# Traverse the directory tree
for dirpath, dirnames, filenames in os.walk(root_directory):
for filename in filenames:
full_path = os.path.join(dirpath, filename)
basename, extension = os.path.splitext(filename)
if extension.lower() in valid_extensions:
basenames_to_paths[basename].append(full_path)
# Filter basenames with more than one file
duplicate_basenames = {basename: paths for basename, paths in basenames_to_paths.items() if len(paths) > 1}
return duplicate_basenames
def get_file_size(path):
try:
return os.path.getsize(path)
except OSError:
return 0
def display_parent_levels(path, levels=2):
# Display 'levels' parent directories above the file path
components = path.split(os.sep)
return os.sep.join(components[-levels:])
def prompt_user_for_selection(paths):
print("Choose which file to keep:")
for i, path in enumerate(paths, start=1):
size_mb = get_file_size(path) / (1024 * 1024)
print(f"{i}. {display_parent_levels(path)} ({size_mb:.2f} MB)")
while True:
try:
choice = int(input("Enter the number of the file to keep (or 0 to skip): "))
if 0 <= choice <= len(paths):
return choice
else:
print("Invalid choice. Please enter a valid number.")
except ValueError:
print("Invalid input. Please enter a number.")
if __name__ == "__main__":
server_directory = r"\\truenas\isolation\videos" # Replace with your server directory
video_extensions = {".mp4", ".mkv", ".avi", ".mov"} # Add more video extensions if needed
duplicate_files = find_duplicate_basenames(server_directory, video_extensions)
if duplicate_files:
for basename, paths in duplicate_files.items():
print(f"\nBasename: {basename}")
choice = prompt_user_for_selection(paths)
if choice == 0:
print("Skipping this duplicate set.")
else:
# Keep the selected file and delete the others
keep_path = paths[choice - 1]
for path in paths:
if path != keep_path:
print(f"Deleting: {display_parent_levels(path)}")
os.remove(path)
print("\nDuplicate files processed.")
else:
print("No duplicate video files found.")