Advertisement
Guest User

Analogue Pocket Memories Cleaner

a guest
Apr 18th, 2024
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | Software | 0 0
  1. import os
  2. import glob
  3. from datetime import datetime
  4.  
  5. #This script will clean the Memories of your Analogue pocket.
  6. #It groups the save states by game name and deletes all but the newest save state for every game.
  7. #Place it on your SD Card inside the Save States folder (SD:/Memories/Save States/).
  8.  
  9. def process_files_in_directory(directory):
  10.     # Get all .sta files in the directory
  11.     all_files = glob.glob(os.path.join(directory, '*.sta'))
  12.  
  13.     # Group files by the characters after "USR_" in their filenames
  14.     file_groups = {}
  15.     for file in all_files:
  16.         filename = os.path.basename(file)
  17.         # Find the position of "USR_" in the filename
  18.         usr_pos = filename.find("USR_")
  19.         if usr_pos != -1:
  20.             # Extract the characters after "USR_"
  21.             key = filename[usr_pos + 4:] # 4 is the length of "USR_"
  22.             if key not in file_groups:
  23.                 file_groups[key] = []
  24.             file_groups[key].append(file)
  25.  
  26.     # For each group, delete all files except for the newest one
  27.     for group in file_groups.values():
  28.         if len(group) > 1: # Only proceed if there's more than one file in the group
  29.             # Sort files by date modified, newest first
  30.             sorted_files = sorted(group, key=lambda x: os.path.getmtime(x), reverse=True)
  31.             # Keep the newest file and delete the rest
  32.             for file in sorted_files[1:]:
  33.                 os.remove(file)
  34.                 print(f"Deleted: {file}")
  35.  
  36. current_folder = os.getcwd()
  37.  
  38. process_files_in_directory(current_folder)
  39.  
  40. for subdir in os.listdir(current_folder):
  41.     subdir_path = os.path.join(current_folder, subdir)
  42.     if os.path.isdir(subdir_path):
  43.         process_files_in_directory(subdir_path)
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement