Skip to content

Instantly share code, notes, and snippets.

@febs
Last active February 11, 2020 11:04
Show Gist options
  • Save febs/e7a5bef23cf332ef724adbbe10cde1e9 to your computer and use it in GitHub Desktop.
Save febs/e7a5bef23cf332ef724adbbe10cde1e9 to your computer and use it in GitHub Desktop.
Script to unzip and sort Gameboy and Gameboy color ROMs for the EzFlash - Jr flash cart so to bypass its limitations
#!/usr/bin/env python3
import os # to create the output folder
import shutil # to copy files into it
import zipfile # to unzip stuff (the EzFlash Jr does not support zipped ROMs)
# let's put a warning here and ask the user if she wants to proceed or not.
try:
input ("""
You are sorting the files in """ + os.getcwd() + """.
Your files will be untouched. You'll find a "sorted" folder containing the relevant files
unzipped and sorted.
Enter to continue or CTRL-C to interrupt.""")
except KeyboardInterrupt:
exit (0)
# let's create a folder with the splitted stuff
sortedDirName = "sorted"
try:
os.mkdir (sortedDirName)
except FileExistsError:
print ("The \"" + sortedDirName + "\" folder already exists. Exiting.")
exit (-1)
# moving all files in the destination directory and unzipping them while I'm at it
root_dir = '.'
for directory, subdirectories, files in os.walk(root_dir):
for file in files:
if file.lower().endswith(".gb") or file.lower().endswith(".gbc"):
# it's an unzipped ROM. Let's also make all extension names lowercase.
basename, extension = os.path.splitext(file)
destination = sortedDirName + "/" + basename + extension.lower()
if not os.path.isfile(destination):
shutil.copyfile((os.path.join(directory, file)), (destination ))
if file.lower().endswith(".zip"):
zip_file = zipfile.ZipFile(file)
for name in zip_file.namelist():
if name.lower().endswith(".gb") or name.lower().endswith(".gbc"):
destination = sortedDirName
if not os.path.isfile(destination):
zip_file.extract(name,destination)
# let's rename extracted stuff with uppercase extension
if not (name.endswith(".gb") or name.endswith (".gbc")):
basename, extension = os.path.splitext(name)
extractedFileFullName = destination + "/" + name
extractedFileFullNewName = destination + "/" + basename + extension.lower()
# print ("renaming " + extractedFileFullName + " in " + extractedFileFullNewName)
os.rename ( extractedFileFullName, extractedFileFullNewName)
# Let's start moving stuff
import numpy
def splitintoeightparts ( path ):
os.chdir (path)
mylist = os.listdir (".")
if (path == sortedDirName):
number_of_elements=8
else:
number_of_elements=9
split = ""
if (len (mylist) >= number_of_elements):
if (len(mylist) > number_of_elements*number_of_elements):
split = numpy.array_split(sorted(mylist, key=str.casefold), number_of_elements)
elif (len(mylist) > number_of_elements/2*number_of_elements/2):
split = numpy.array_split(sorted(mylist, key=str.casefold), number_of_elements/2)
elif (len(mylist) > number_of_elements/4*number_of_elements/4):
split = numpy.array_split(sorted(mylist, key=str.casefold), number_of_elements/4)
for subgroup in split:
# building a folder named after the first and last element of each subset
first_rom, extension = os.path.splitext(subgroup[0])
last_rom, extension = os.path.splitext(subgroup[len(subgroup)-1])
dirname = first_rom[0:10].strip() + ".." + last_rom[0:10].strip()
try:
os.mkdir (dirname)
except FileExistsError:
print ("The \"" + dirname + "\" folder already exists. ")
for romfile in subgroup:
shutil.move(romfile, dirname)
splitintoeightparts(dirname)
os.chdir("..")
splitintoeightparts(sortedDirName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment