Friday, June 28, 2024

Powershell script to clean MAME roms

Here's a powershell script to identify "parent" MAME roms, moving all unique ones to a "clean" folder.

- Create a "MoveNonClones.ps1" file in your MAME rom folder:

# Create the "clean" folder if it doesn't exist

$cleanFolder = Join-Path -Path (Get-Location) -ChildPath "clean"

if (-not (Test-Path -Path $cleanFolder)) {

    New-Item -ItemType Directory -Path $cleanFolder

}

# Get all .zip files in the current directory

$files = Get-ChildItem -Path . -Filter *.zip

# Remove the .zip extension from the filenames

$fileNames = $files | ForEach-Object { [System.IO.Path]::GetFileNameWithoutExtension($_.Name) }

# Initialize an array to store the parents to keep

$parentsToKeep = @()

Write-Output "Identifying parent ROMs..."

$totalFiles = $fileNames.Count

$currentFile = 0

# Iterate through each file name

while ($fileNames.Count -gt 0) {

    $currentFile++

    # Take the first file name from the list

    $fileName = $fileNames[0]

    # Find all files that start with the current file name

    $matchingFiles = $fileNames | Where-Object { $_ -like "$fileName*" }

    # Find the one with the smallest number of characters

    $parentToKeep = $matchingFiles | Sort-Object { $_.Length } | Select-Object -First 1

    # Add the parent to the list of parents to keep

    if (-not $parentsToKeep.Contains($parentToKeep)) {

        $parentsToKeep += $parentToKeep

        # Find the corresponding file object

        $parentFile = $files | Where-Object { [System.IO.Path]::GetFileNameWithoutExtension($_.Name) -eq $parentToKeep }

        # Move the parent file to the "clean" folder

        foreach ($file in $parentFile) {

            $destination = Join-Path -Path $cleanFolder -ChildPath $file.Name

            Move-Item -Path $file.FullName -Destination $destination

            Write-Output "Identified and moved parent ROM: $($file.Name)"

        }

    }

    # Remove the matching files from the list of file names

    $fileNames = $fileNames | Where-Object { $_ -notin $matchingFiles }

    Write-Progress -Activity "Identifying parent ROMs" -Status "$currentFile of $totalFiles" -PercentComplete (($currentFile / $totalFiles) * 100)

}

Write-Output "Process completed. Moved $($parentsToKeep.Count) parent ROMs to the clean folder."

 - Run the powershell script!

This will identify all "parent" roms (basically, all the ones that are either unique OR have multiple files with the same "StartsWith()" filename (e.g. 3countb.zip is the main file and 3countba.zip is a clone version) and move the unique ones to a "clean" folder inside your current execution folder.

Its not perfect (it can remove versions of games as it does not know ddragon and ddragon2 are different games altogether) but...it does clean up the library quite a bit!

No comments:

Post a Comment