Skip to content

Instantly share code, notes, and snippets.

@nalf3in
Last active July 8, 2020 22:34
Show Gist options
  • Save nalf3in/f5b8b12172839d06f5e77352ebe4bdd0 to your computer and use it in GitHub Desktop.
Save nalf3in/f5b8b12172839d06f5e77352ebe4bdd0 to your computer and use it in GitHub Desktop.
Quick script to rename beatsaber songs folders to something a bit better
import os
import shutil
import json
import traceback
import time
from datetime import datetime
import tkinter as tk
from tkinter import filedialog
# If this is true, overwrite an folder with the same name if the other one is newer
OVERWRITE = True
def rename_folders(path=os.getcwd()):
os.chdir(path)
successes = []
ignored = []
errors = []
dir_entries = os.scandir(path)
for entry in dir_entries:
if entry.is_dir():
try:
# The file needs to be closed before renaming :/
# thus the variable declaration here
artist_name = None
song_name = None
# We are able to read the file !
with open(entry.name + "/info.dat", "r") as file:
json_object = json.load(file)
artist_name = json_object["_songAuthorName"]
song_name = json_object["_songName"]
if artist_name != None:
new_folder_name = f"{artist_name} - {song_name}"
folder_exists = os.path.exists(new_folder_name)
if folder_exists: # We only do something if the folder doesn't have the same name
if entry.name == new_folder_name:
ignored.append(f"Folder [{entry.name}] already has the correct name")
elif OVERWRITE and entry.name != new_folder_name:
if os.path.getctime(entry.name) > os.path.getctime(new_folder_name):
shutil.rmtree(new_folder_name)
os.rename(entry.name, new_folder_name)
successes.append(f"Folder [{entry.name}], [{new_folder_name}] was overwritten")
else:
os.rename(entry.name, new_folder_name)
successes.append(f"Folder [{entry.name}], was renamed to [{new_folder_name}]")
except AttributeError:
error = traceback.format_exc().splitlines()[-1:][0]
errors.append(f"Folder [{entry.name}], don't have the Json attributes")
except OSError:
error = traceback.format_exc().splitlines()[-1:][0]
errors.append(f"Error for folder [{entry.name}], {error}")
return successes, ignored, errors
def print_output(successes, ignored, errors):
print("___________________________________________________________________\n")
now = datetime.now()
time = f"{now.year}-{now.month}-{now.month}-{now.day}, {now.hour}:{now.second}"
print(f"beatsaber_folder_renamer {time}\n")
print("Successes")
for op in successes:
print(f" {op}\n")
print("Ignored")
for op in ignored:
print(f" {op}\n")
print("Errors")
for op in errors:
print(f" {op}\n")
# Prompt window's file dialog to get the songs directory path
def get_songs_path():
root = tk.Tk()
root.withdraw()
return filedialog.askdirectory()
# Start of the program here
if __name__ == "__main__":
songs_path = get_songs_path()
successes, ignored, errors = rename_folders(songs_path)
print_output(successes, ignored, errors)
input("Press enter to continue")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment