My Computer World









If you have recently bought your "The Spectrum" and have some games that you want to copy to a USB to be able to play on "The Spectrum" and then create sub folders to spilt up the folders, use this windows power-script to copy them for you.


# Define source and destination directories

$sourceDir = "D:\ZX Spectrum files\games"  # Replace with your source directory path

$destDir = "E:\"  # Replace with your destination directory path


# Validate that the source directory exists

if (!(Test-Path $sourceDir)) {

    Write-Host "Source directory does not exist: $sourceDir" -ForegroundColor Red

    exit

}


# Create a map of prefixes to their corresponding directories

$prefixGroups = @{

    "0-9" = { $args[0] -match "^[0-9]" }

    "A"   = { $args[0] -match "^A" }

    "B"   = { $args[0] -match "^B" }

    "C"   = { $args[0] -match "^C" }

    "D"   = { $args[0] -match "^D" }

    "E"   = { $args[0] -match "^E" }

    "F"   = { $args[0] -match "^F" }

    "G"   = { $args[0] -match "^G" }

    "H"   = { $args[0] -match "^H" }

    "I"   = { $args[0] -match "^I" }

    "J"   = { $args[0] -match "^J" }

    "K"   = { $args[0] -match "^K" }

    "L"   = { $args[0] -match "^L" }

    "M"   = { $args[0] -match "^M" }

    "N"   = { $args[0] -match "^N" }

    "O"   = { $args[0] -match "^O" }

    "P"   = { $args[0] -match "^P" }

    "Q"   = { $args[0] -match "^Q" }

    "R"   = { $args[0] -match "^R" }

    "S"   = { $args[0] -match "^S" }

    "T"   = { $args[0] -match "^T" }

    "U"   = { $args[0] -match "^U" }

    "V"   = { $args[0] -match "^V" }

    "W"   = { $args[0] -match "^W" }

    "X"   = { $args[0] -match "^X" }

    "Y"   = { $args[0] -match "^Y" }

    "Z"   = { $args[0] -match "^Z" }

}


# Get files from the source directory

$files = Get-ChildItem -Path $sourceDir -File


if ($files.Count -eq 0) {

    Write-Host "No files found in the source directory." -ForegroundColor Yellow

    exit

}


# Copy files to corresponding directories

foreach ($file in $files) {

    $fileName = $file.Name

    $matched = $false


    foreach ($group in $prefixGroups.Keys) {

        if ($prefixGroups[$group].Invoke($fileName)) {

            # Create the destination folder if it doesn't exist

            $destGroupDir = Join-Path $destDir $group

            if (!(Test-Path $destGroupDir)) {

                New-Item -ItemType Directory -Path $destGroupDir | Out-Null

            }


            # Define the destination file path

            $destFilePath = Join-Path $destGroupDir $fileName


            # Copy the file and overwrite if it already exists

            Copy-Item -LiteralPath $file.FullName -Destination $destFilePath -Force

            $matched = $true

            break

        }

    }


    if (-not $matched) {

        Write-Host "No matching group for file: $fileName" -ForegroundColor Yellow

    }

}


Write-Host "Files have been copied to their corresponding directories." -ForegroundColor Green








 

Copyright © 2024 MyComputerWorld.co.uk

My Computer World
Retro Gaming in a modern world.