Skip to content

Instantly share code, notes, and snippets.

@Pxtl
Created December 26, 2019 05:22
Show Gist options
  • Save Pxtl/daed801def23a08cb81c6b8b771d69d6 to your computer and use it in GitHub Desktop.
Save Pxtl/daed801def23a08cb81c6b8b771d69d6 to your computer and use it in GitHub Desktop.
Rename Bin/Cue file using Powershell - designed for PS1 files
# for a given cue/bin pair, rename the bin file, rename the cue file with the corresponding name,
# and update the bin reference within the cue file to match
[cmdletbinding()]
param(
[Parameter(Mandatory)][io.FileInfo]$sourceBinPath,
[Parameter(Mandatory)][string]$newName
)
$sourceCuePath = [io.path]::ChangeExtension($sourceBinPath, 'cue')
write-verbose("sourceCuePath: $sourceCuePath")
$sourceBinName = [io.path]::GetFileName($sourceBinPath)
write-verbose("sourceBinName: $sourceBinName")
$newBinName = [io.path]::ChangeExtension($newName, 'bin')
write-verbose("newBinName: $newBinName")
$newBinNameNoDirectory = [io.path]::GetFileName($newBinName)
$newCueName = [io.path]::ChangeExtension($newName, 'cue')
write-verbose("newCueName: $newCueName")
$sourceBinRegex = "FILE `"$([Regex]::Escape($sourceBinName))`" BINARY"
write-verbose("sourceBinRegex: $sourceBinRegex")
#replace the reference to the bin within the cue file to the new name
((Get-Content -path $sourceCuePath -Raw) -replace $sourceBinRegex,"FILE `"$newBinNameNoDirectory`" BINARY") | Set-Content -Path $sourceCuePath
Rename-Item $sourceBinPath $newBinName
Rename-Item $sourceCuePath $newCueName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment