import xml.etree.ElementTree as ET
import os
import sys
def generate_copy_commands(mame_xml_path, roms_dir, output_dir, output_file="copy_vertical_roms.bat"):
"""
Generates a batch file with copy commands for vertical MAME ROMs.
Args:
mame_xml_path (str): Path to the mame.xml file generated by 'mame -listxml'.
roms_dir (str): Path to your MAME ROMs directory (e.g., 'L:\\MAME ROMs').
output_dir (str): The name of the new folder for vertical ROMs (e.g., 'vertical').
output_file (str): The name of the batch file to create.
"""
try:
# Parse the MAME XML file
tree = ET.parse(mame_xml_path)
root = tree.getroot()
print(f"Successfully parsed '{mame_xml_path}'.")
except FileNotFoundError:
print(f"Error: mame.xml not found at '{mame_xml_path}'. Please ensure you ran 'mame.exe -listxml > mame.xml' and that the path is correct.")
return
except ET.ParseError:
print(f"Error: Could not parse '{mame_xml_path}'. Ensure it's a valid XML file and not empty.")
return
# Construct the full path for the output directory
target_roms_dir = os.path.join(roms_dir, output_dir)
# List to store the generated copy commands
copy_commands = []
# Add a command to create the output directory
copy_commands.append(f"mkdir \"{target_roms_dir}\" 2>nul") # 2>nul suppresses error if dir exists
all_games_count = 0
vertical_games_count = 0
# Iterate through each 'machine' (game) in the XML
for machine in root.findall('machine'):
all_games_count += 1
game_name = machine.get('name')
if not game_name:
continue # Skip if game name is missing
# Find the 'display' element for orientation
display_element = machine.find('display')
is_vertical = False
if display_element is not None:
rotate_attribute = display_element.get('rotate')
# A game is vertical if rotate is '90' or '270'
if rotate_attribute == '90' or rotate_attribute == '270':
is_vertical = True
if is_vertical:
vertical_games_count += 1
# Construct the full path to the source ROM file
source_rom_path = os.path.join(roms_dir, f"{game_name}.zip")
# Construct the full path to the destination ROM file
destination_rom_path = os.path.join(target_roms_dir, f"{game_name}.zip")
# Add the copy command
copy_commands.append(f"copy /Y \"{source_rom_path}\" \"{destination_rom_path}\"")
print(f"Total machines (games) found in mame.xml: {all_games_count}")
print(f"Vertical games identified: {vertical_games_count}")
# Write the commands to the batch file
try:
with open(output_file, 'w') as f:
for command in copy_commands:
f.write(command + '\n')
if vertical_games_count > 0:
print(f"\nSuccessfully generated '{output_file}' with {vertical_games_count} copy commands.")
print(f"The new folder '{target_roms_dir}' will be created (if it doesn't exist) when you run the batch file.")
print("Review the batch file before running it to ensure it does what you expect.")
else:
print(f"\nNo vertical games were found with 'rotate=\"90\"' or 'rotate=\"270\"' in the 'display' tag.")
print(f"'{output_file}' was created but contains only the directory creation command.")
print("Please double-check your 'mame.xml' file to ensure it contains games matching this criteria.")
except IOError as e:
print(f"Error: Could not write to '{output_file}'. {e}")
if __name__ == "__main__":
# Define default paths based on user's request
default_mame_xml = "mame.xml" # Assumes mame.xml is in the same directory as the script or MAME.exe
default_roms_dir = r"L:\MAME ROMs" # Use raw string for backslashes
default_output_folder_name = "vertical" # Changed default to 'vertical'
print("This script will create a batch file to copy vertical MAME ROMs.")
print("------------------------------------------------------------------")
print(f"Default MAME XML file path: '{default_mame_xml}'")
print(f"Default MAME ROMs directory: '{default_roms_dir}'")
print(f"Default output folder name: '{default_output_folder_name}'")
print("------------------------------------------------------------------")
# Allow user to provide custom paths via command line arguments or use defaults
mame_xml_path = input(f"Enter path to mame.xml (press Enter for default '{default_mame_xml}'): ").strip() or default_mame_xml
roms_dir = input(f"Enter your MAME ROMs directory (press Enter for default '{default_roms_dir}'): ").strip() or default_roms_dir
output_folder_name = input(f"Enter the name for the new vertical ROMs folder (press Enter for default '{default_output_folder_name}'): ").strip() or default_output_folder_name
generate_copy_commands(mame_xml_path, roms_dir, output_folder_name)
print("\nTo execute the copy operations, run the generated 'copy_vertical_roms.bat' file.")
print("Make sure the 'mame.xml' file is accessible from where you run this script or provide its full path.")
print("The script expects ROMs to be in .zip format.")