Skip to content

Instantly share code, notes, and snippets.

@dually8
Last active December 7, 2024 21:06
Show Gist options
  • Save dually8/0b39394ed8d34038b5b2332922f8c6c8 to your computer and use it in GitHub Desktop.
Save dually8/0b39394ed8d34038b5b2332922f8c6c8 to your computer and use it in GitHub Desktop.
How To Merge Partial Files

How to merge partial files

Taken from the nintendo switch dumping guide wiki (or other new guide)

Windows

  1. Open cmd.exe, use cd to change to the directory of the files you want to merge.
    • PROTIP: Leaving only the part files here will make this process easier.
  2. Type copy /b * name.ext where name is the name you want to give the file and .ext is the file extension you want to use
    • e.g. copy /b * game.xci

MacOS/Linux

  1. Open the terminal and cd into the directory that has the part files
  2. Type cat * > name.ext where name is the name you want to give the file and .ext is the file extension you want to use
    • e.g. cat * > game.xci

PowerShell Core (aka: pwsh)

PowerShell Core Script

This isn't thoroughly tested so your mileage may vary. Just copy the contents of this and throw it in your powershell profile or create a file "Merge-Files.ps1" and paste the contents in there to use it. You can use it like so

Merge-Files -Filter "*.xc*" -OutputFile "game.xci"
# Example usage:
# Merge-Files -Filter "*.xc*" -OutputFile "game.xci"
# Merge-Files -Filter "*.mp3" -OutputFile "combined.mp3"
# Merge-Files -Filter "*.mp4" -OutputFile "D:\Videos\combined.mp4" -Path "D:\Downloads"
# Merge-Files -Filter "*.bin" -OutputFile "output.bin"
function Merge-Files {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$OutputFile, # The name of the output file
        [Parameter(Mandatory = $false)]
        [string]$Path = ".", # Directory containing the partial files (defaults to the current directory)
        [Parameter(Mandatory = $false)]
        [string]$Filter = "*" # The filter to use (e.g. "*.mp3")
    )
    try {
        # Ensure output file does not already exist
        if (Test-Path $OutputFile) {
            Write-Error "Output file '$OutputFile' already exists. Please delete or specify a different name."
            exit 1
        }

        # Get all files in the input directory
        $partialFiles = Get-ChildItem -Path $Path -Filter $Filter -File | Sort-Object Name

        if ($partialFiles.Count -eq 0) {
            Write-Error "No files with '$Filter' found in the directory '$Path'."
            exit 1
        }

        # Open the output file in binary write mode
        $outputStream = [System.IO.FileStream]::new($OutputFile, [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write)

        try {
            # Buffer size for chunking (e.g., 4 KB)
            $bufferSize = 4096
            $buffer = New-Object byte[] $bufferSize

            foreach ($file in $partialFiles) {
                Write-Host "Merging: $($file.Name)"
                # Open each input file in binary read mode
                $inputStream = [System.IO.FileStream]::new($file.FullName, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read)
                try {
                    while (($bytesRead = $inputStream.Read($buffer, 0, $bufferSize)) -gt 0) {
                        $outputStream.Write($buffer, 0, $bytesRead)
                    }
                }
                finally {
                    $inputStream.Close()
                    Write-Host "Merged in: $($file.Name)"
                }
            }

            Write-Host "All files merged successfully into '$OutputFile'."
        }
        finally {
            $outputStream.Close()
        }
    }
    catch {
        Write-Error "An error occurred: $_"
    }
}
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment