Skip to content

Instantly share code, notes, and snippets.

@Lambdanaut
Last active February 6, 2024 01:06
Show Gist options
  • Select an option

  • Save Lambdanaut/a9c7992f68c535b3666849983ccf50b6 to your computer and use it in GitHub Desktop.

Select an option

Save Lambdanaut/a9c7992f68c535b3666849983ccf50b6 to your computer and use it in GitHub Desktop.
zoia-ctrl.py
"""
zoia-ctrl.py
==============
Command line script to manage ZOIA file patches in a directory
Usage
-----
For renaming all files sequentially:
> python3 zoia-ctrl.py rename /path/to/your/zoia/files
For moving a file:
> python3 zoia-ctrl.py move /path/to/your/zoia/files source_number target_number
"""
import os
import re
import argparse
def rename_zoia_files(directory):
pattern = re.compile(r'^(\d{3})_zoia_(.+)\.bin$')
zoia_files = []
for filename in os.listdir(directory):
if pattern.match(filename):
zoia_files.append(filename)
total_files = len(zoia_files)
zoia_files.sort(key=lambda x: int(pattern.match(x).group(1)))
total_renamed = 0
next_number = 0
for filename in zoia_files:
name = pattern.match(filename).group(2)
new_number = f"{next_number:03d}"
new_filename = f"{new_number}_zoia_{name}.bin"
if filename != new_filename:
os.rename(os.path.join(directory, filename), os.path.join(directory, new_filename))
print(f"Renamed: {filename} -> {new_filename}")
total_renamed += 1
next_number += 1
return (total_renamed, total_files)
def move_zoia_file(directory, source, target):
pattern = re.compile(r'^(\d{3})_zoia_(.+)\.bin$')
source = f"{int(source):03d}"
target = f"{int(target):03d}"
source_file = None
# Find the source file
for filename in os.listdir(directory):
if pattern.match(filename) and pattern.match(filename).group(1) == source:
source_file = filename
break
if source_file:
# Generate a temporary filename for the move operation
temp_filename = f"{target}_zoia_{pattern.match(source_file).group(2)}.bin"
os.rename(os.path.join(directory, source_file), os.path.join(directory, temp_filename))
print(f"Moved: {source} -> {temp_filename}")
# After moving the file, reorder all files in the directory
rename_zoia_files(directory)
print("Files have been reordered.")
else:
print("Source file does not exist.")
def main():
parser = argparse.ArgumentParser(description='Manage ZOIA patch files in a directory.')
parser.add_argument('command', type=str, help='Command to execute (rename, move)')
parser.add_argument('directory', type=str, help='Directory containing ZOIA patch files')
parser.add_argument('source', type=str, nargs='?', help='Source number for the move command')
parser.add_argument('target', type=str, nargs='?', help='Target number for the move command')
args = parser.parse_args()
if args.command == 'rename':
total_renamed, total_files = rename_zoia_files(args.directory)
print(f"Renaming Complete: {total_renamed}/{total_files} renamed.")
elif args.command == 'move':
if args.source and args.target:
move_zoia_file(args.directory, args.source, args.target)
else:
print("Move command requires both source and target numbers.")
else:
print("Invalid command. Please use 'rename' or 'move'.")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment