pip -m install send2trash colorama
python processDupesInt.py "C:/Path/to/directory"
Last active
February 23, 2024 02:32
-
-
Save TheExpertNoob/6bfe01a0f5a618907625268d0313deb6 to your computer and use it in GitHub Desktop.
Interactive Duplication Processor by TitleID
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
import argparse | |
from collections import defaultdict | |
from send2trash import send2trash | |
import time | |
from colorama import init, Fore, Back, Style | |
# Initialize colorama with ANSI color support | |
init(convert=True, autoreset=True) | |
def clear_screen(): | |
# Platform-specific method to clear the screen | |
os.system('cls' if os.name == 'nt' else 'clear') | |
def find_and_prompt(directory): | |
# Regular expression to match filenames with 16 hexadecimal digits between brackets | |
pattern = re.compile(r'\[([0-9a-fA-F]{16})\]') | |
# Dictionary to store filenames based on their matched patterns | |
file_dict = defaultdict(list) | |
# Traverse the specified directory | |
for root, dirs, files in os.walk(directory): | |
for filename in files: | |
match = pattern.search(filename) | |
if match: | |
# Append the filename to the list corresponding to the matched pattern | |
file_dict[match.group(1)].append(os.path.relpath(os.path.join(root, filename), directory)) | |
# Identify and prompt for duplicates | |
for hex_digits, filenames in file_dict.items(): | |
if len(filenames) > 1: | |
print(f'Duplicates for pattern [{hex_digits}]:') | |
for i, filename in enumerate(filenames): | |
print(f' {i + 1}. {filename}') | |
# Prompt user for input | |
choice = input("Choose the file to keep (enter the corresponding number): ") | |
try: | |
choice_index = int(choice) - 1 | |
if 0 <= choice_index < len(filenames): | |
# Keep the selected file and move the others to the recycle bin | |
for i, filename in enumerate(filenames): | |
if i != choice_index: | |
send2trash(os.path.join(directory, filename)) | |
print(Back.RED + Fore.WHITE + f'Moved to recycle bin: {filename}' + Style.RESET_ALL) | |
print(Back.GREEN + Fore.BLACK + f'Kept: {filenames[choice_index]}' + Style.RESET_ALL) | |
input("Press Enter to continue...") | |
time.sleep(1) # Pause for 1 second to prevent double taps | |
clear_screen() # Clear the screen after 1-second pause | |
else: | |
print("Invalid choice. Skipping this group.") | |
except ValueError: | |
print("Invalid input. Skipping this group.") | |
if __name__ == "__main__": | |
# Set up command-line argument parser | |
parser = argparse.ArgumentParser(description='Interactively find and choose which files to keep in duplicate groups.') | |
parser.add_argument('directory', help='Target directory to scan for duplicates.') | |
# Parse command-line arguments | |
args = parser.parse_args() | |
# Call the function to find and prompt for duplicates | |
find_and_prompt(args.directory) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment