Guest User

WallpaperRenamerPlus

a guest
Jul 30th, 2024
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.79 KB | Source Code | 0 0
  1. import os
  2. import random
  3. import string
  4. import hashlib
  5. import time
  6.  
  7. # Directory containing the files
  8. directory = r'C:\Path\to\your\wallpapers'
  9.  
  10. """
  11. This script renames files in a specified directory to random 6-character alphanumeric filenames consisting
  12. of uppercase letters and digits. It includes the following features:
  13.  
  14. 1. Skips renaming files that already have a 6-character long name.
  15. 2. Ensures that no two files will be renamed to the same filename by checking for existing filenames
  16.   and generating new ones until a unique name is found.
  17. 3. Generates Blake2b hashes for each file to check for duplicate contents and deletes duplicates.
  18. 4. Prints a message for each file it renames, indicates when it skips a file, and deletes duplicates.
  19. 5. Provides a summary at the end of the script, indicating how many files it scanned, skipped, renamed, deleted,
  20.   and the count of each file type.
  21. 6. Displays the total time the script took to run.
  22. """
  23.  
  24. # Function to generate a random filename
  25. def generate_random_filename(length=6):
  26.     characters = string.ascii_uppercase + string.digits
  27.     return ''.join(random.choice(characters) for _ in range(length))
  28.  
  29. # Function to calculate the Blake2b hash of a file
  30. def calculate_hash(file_path):
  31.     hasher = hashlib.blake2b()
  32.     with open(file_path, 'rb') as f:
  33.         for chunk in iter(lambda: f.read(4096), b""):
  34.             hasher.update(chunk)
  35.     return hasher.hexdigest()
  36.  
  37. # Function to rename files in the directory
  38. def rename_files(directory):
  39.     start_time = time.time()
  40.     scanned_count = 0
  41.     skipped_count = 0
  42.     renamed_count = 0
  43.     duplicate_count = 0
  44.     file_type_counts = {}
  45.     hash_dict = {}
  46.  
  47.     for filename in os.listdir(directory):
  48.         file_path = os.path.join(directory, filename)
  49.         if os.path.isfile(file_path):
  50.             scanned_count += 1
  51.             file_name, file_extension = os.path.splitext(filename)
  52.  
  53.             # Count file types
  54.             file_extension = file_extension.lower()
  55.             if file_extension not in file_type_counts:
  56.                 file_type_counts[file_extension] = 0
  57.             file_type_counts[file_extension] += 1
  58.  
  59.             # Calculate Blake2b hash
  60.             file_hash = calculate_hash(file_path)
  61.  
  62.             if file_hash in hash_dict:
  63.                 duplicate_count += 1
  64.                 os.remove(file_path)
  65.                 print(f'Deleting "{filename}" (duplicate content found)')
  66.                 continue
  67.             hash_dict[file_hash] = filename
  68.  
  69.             if len(file_name) == 6:
  70.                 skipped_count += 1
  71.                 print(f'Skipping "{filename}" (already 6 characters long) Hash: {file_hash}')
  72.                 continue
  73.  
  74.             # Generate a unique filename
  75.             new_filename = generate_random_filename() + file_extension
  76.             new_file_path = os.path.join(directory, new_filename)
  77.             while os.path.exists(new_file_path):
  78.                 new_filename = generate_random_filename() + file_extension
  79.                 new_file_path = os.path.join(directory, new_filename)
  80.            
  81.             os.rename(file_path, new_file_path)
  82.             renamed_count += 1
  83.             print(f'Renaming "{filename}" to "{new_filename}"')
  84.  
  85.     end_time = time.time()
  86.     total_time = end_time - start_time
  87.  
  88.     # Summary
  89.     print("\nSummary:")
  90.     print(f"Total files scanned: {scanned_count}")
  91.     print(f"Files skipped: {skipped_count}")
  92.     print(f"Files renamed: {renamed_count}")
  93.     print(f"Duplicates deleted: {duplicate_count}")
  94.     for file_type, count in file_type_counts.items():
  95.         print(f"{file_type.upper()} files scanned: {count}")
  96.     print(f"Total time taken: {total_time:.2f} seconds")
  97.  
  98. # Run the renaming process
  99. rename_files(directory)
  100.  
  101. # Pause at the end
  102. input("Press Enter to exit...")
  103.  
Advertisement
Add Comment
Please, Sign In to add comment