Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import glob
- from datetime import datetime
- #This script will clean the Memories of your Analogue pocket.
- #It groups the save states by game name and deletes all but the newest save state for every game.
- #Place it on your SD Card inside the Save States folder (SD:/Memories/Save States/).
- def process_files_in_directory(directory):
- # Get all .sta files in the directory
- all_files = glob.glob(os.path.join(directory, '*.sta'))
- # Group files by the characters after "USR_" in their filenames
- file_groups = {}
- for file in all_files:
- filename = os.path.basename(file)
- # Find the position of "USR_" in the filename
- usr_pos = filename.find("USR_")
- if usr_pos != -1:
- # Extract the characters after "USR_"
- key = filename[usr_pos + 4:] # 4 is the length of "USR_"
- if key not in file_groups:
- file_groups[key] = []
- file_groups[key].append(file)
- # For each group, delete all files except for the newest one
- for group in file_groups.values():
- if len(group) > 1: # Only proceed if there's more than one file in the group
- # Sort files by date modified, newest first
- sorted_files = sorted(group, key=lambda x: os.path.getmtime(x), reverse=True)
- # Keep the newest file and delete the rest
- for file in sorted_files[1:]:
- os.remove(file)
- print(f"Deleted: {file}")
- current_folder = os.getcwd()
- process_files_in_directory(current_folder)
- for subdir in os.listdir(current_folder):
- subdir_path = os.path.join(current_folder, subdir)
- if os.path.isdir(subdir_path):
- process_files_in_directory(subdir_path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement