Skip to content

Instantly share code, notes, and snippets.

@GregHilston
Last active March 3, 2024 10:10
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save GregHilston/c0f1a6e418c7909e508758bfd7373d88 to your computer and use it in GitHub Desktop.
Save GregHilston/c0f1a6e418c7909e508758bfd7373d88 to your computer and use it in GitHub Desktop.
Deletes all files that are missing a given string in their name
# Hey everyone, I was having a tough time cleaning up my ROMs, as there would be multiple versions of the same game, like:
# Super-Smash(Japan).rom
# Super-Smash(Europe).rom
# Super-Smash(USA).rom
# I ended up writing this small Python (3.6) script that you can use to cleanup duplicate files.
# Execution looks like this, if you want to keep only USA files:
# python3.6 file_cleaner.py "USA" /path/to/ROMs --dry
# Some sample output may look like this:
# Dry run detected, no modifications will be made
# Would delete:
# 'test/JAPAN_file.zip'
# Would delete 1 files out of 2 files
# I suggest running with
# --dry
# at first to verify output.
# I hope this helps someone!
# Also, obligatory: I take no responsibility for usage of this script, use at your own risk.
import sys
import os
directory_path = ""
substring = ""
dry_run = False
if len(sys.argv) == 2 and sys.argv[1] == "--opt":
print("$ python3 [substring that must be in files] [path to directory] [--dry for dry run]")
sys.exit(-1)
if len(sys.argv) < 3:
print("Missing substring to match against or directory to run in")
sys.exit(-2)
substring = sys.argv[1]
# print(f"substring detected: '{substring}'")
directory_path = sys.argv[2]
# print(f"directory_path detected: '{directory_path}'")
if len(sys.argv) == 4 and sys.argv[3] == "--dry":
print("Dry run detected, no modifications will be made")
dry_run = sys.argv[2]
else:
print("Live run detected, modifications will be made")
if not dry_run:
while(input("Continue? [y]") != 'y'):
continue
directory = os.fsencode(directory_path).decode("utf-8")
total_count = 0
delete_count = 0
if dry_run:
print("Would delete:")
else:
print("Deleting:")
for file in os.listdir(directory):
total_count += 1
filename = os.fsdecode(file)
if substring not in filename:
delete_count += 1
full_file_path = os.path.join(directory, filename)
print(f"\t'{full_file_path}'")
if not dry_run:
os.remove(full_file_path)
if dry_run:
print(f"Would delete {delete_count} files out of {total_count} files")
else:
print(f"Deleted {delete_count} files out of {total_count} files")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment