Guest User

Untitled

a guest
Jul 24th, 2024
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.73 KB | Source Code | 0 0
  1. import os
  2. import random
  3. import string
  4. import zlib
  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 CRC checksums 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 CRC checksum of a file
  30. def calculate_crc(file_path):
  31.     prev = 0
  32.     for eachLine in open(file_path, "rb"):
  33.         prev = zlib.crc32(eachLine, prev)
  34.     return "%X" % (prev & 0xFFFFFFFF)
  35.  
  36. # Function to rename files in the directory
  37. def rename_files(directory):
  38.     start_time = time.time()
  39.     scanned_count = 0
  40.     skipped_count = 0
  41.     renamed_count = 0
  42.     duplicate_count = 0
  43.     file_type_counts = {}
  44.     crc_dict = {}
  45.  
  46.     for filename in os.listdir(directory):
  47.         file_path = os.path.join(directory, filename)
  48.         if os.path.isfile(file_path):
  49.             scanned_count += 1
  50.             file_name, file_extension = os.path.splitext(filename)
  51.  
  52.             # Count file types
  53.             file_extension = file_extension.lower()
  54.             if file_extension not in file_type_counts:
  55.                 file_type_counts[file_extension] = 0
  56.             file_type_counts[file_extension] += 1
  57.  
  58.             # Calculate CRC checksum
  59.             file_crc = calculate_crc(file_path)
  60.  
  61.             if file_crc in crc_dict:
  62.                 duplicate_count += 1
  63.                 os.remove(file_path)
  64.                 print(f'Deleting "{filename}" (duplicate content found)')
  65.                 continue
  66.             crc_dict[file_crc] = filename
  67.  
  68.             if len(file_name) == 6:
  69.                 skipped_count += 1
  70.                 print(f'Skipping "{filename}" (already 6 characters long) CRC: {file_crc}')
  71.                 continue
  72.  
  73.             # Generate a unique filename
  74.             new_filename = generate_random_filename() + file_extension
  75.             new_file_path = os.path.join(directory, new_filename)
  76.             while os.path.exists(new_file_path):
  77.                 new_filename = generate_random_filename() + file_extension
  78.                 new_file_path = os.path.join(directory, new_filename)
  79.            
  80.             os.rename(file_path, new_file_path)
  81.             renamed_count += 1
  82.             print(f'Renaming "{filename}" to "{new_filename}"')
  83.  
  84.     end_time = time.time()
  85.     total_time = end_time - start_time
  86.  
  87.     # Summary
  88.     print("\nSummary:")
  89.     print(f"Total files scanned: {scanned_count}")
  90.     print(f"Files skipped: {skipped_count}")
  91.     print(f"Files renamed: {renamed_count}")
  92.     print(f"Duplicates deleted: {duplicate_count}")
  93.     for file_type, count in file_type_counts.items():
  94.         print(f"{file_type.upper()} files scanned: {count}")
  95.     print(f"Total time taken: {total_time:.2f} seconds")
  96.  
  97. # Run the renaming process
  98. rename_files(directory)
  99.  
  100. # Pause at the end
  101. input("Press Enter to exit...")
  102.  
Advertisement
Add Comment
Please, Sign In to add comment