Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PowerShell bin cue rename
# 2022-05-31
<#
PS> Get-ExecutionPolicy # PowerShell 5.1 (default in Win10)
Restricted
PS> Set-ExecutionPolicy Unrestricted -Force # as admin
PS> Get-ExecutionPolicy # PowerShell 7.2.3 (installed in Win10)
RemoteSigned
#>
<#
https://docs.microsoft.com/en-us/powershell/scripting/dev-cross-plat/vscode/understanding-file-encoding
https://docs.microsoft.com/en-us/powershell/scripting/whats-new/differences-from-windows-powershell
In PowerShell 6+, the default encoding 'utf8NoBOM' is UTF-8 without BOM on all platforms.
But option 'utf8NoBOM' is not available in previous PowerShell 5.1 (in Win10 default).
In Windows PowerShell 5, the default encoding 'Default' (usually ANSI) is usually Windows-1252,
an extension of latin-1, also known as ISO 8859-1. Options 'UTF8' in PowerShell 5.1 is with BOM.
!!! Change $OutputEncoding to use UTF-8 NoBOM encoding rather than ASCII !!!
The previous encoding, ASCII (7-bit), would result in incorrect alteration of the output in some cases.
Making UTF-8 NoBOM the default preserves Unicode output with an encoding supported by most tools
and operating systems.
'utf8NoBOM' UTF-8 without BOM is best choice and is default in PowerShell 6+.
#>
<#
https://renenyffenegger.ch/notes/Windows/PowerShell/command-inventory/noun/childItem/get/index
'-Filter' is faster, and the results are more predictable.
'-Filter' accepts only a single string. '-Include' accepts multiple values.
Get-ChildItem -File -Recurse -Filter "*.ogg"
#>
# https://gist.github.com/Pxtl/daed801def23a08cb81c6b8b771d69d6
#Requires -Version 6.0
Get-ChildItem -File -Recurse -Include "*.ogg", "*.mp3" | ForEach-Object {
$BinName = $_.Name
$BinFullName = $_.FullName
Write-Host("bin file :", $BinFullName)
$CueFullName = [System.IO.Path]::ChangeExtension($BinFullName, 'cue')
$CueTest = [System.IO.Path]::ChangeExtension($BinFullName, 'test.cue') # test
Write-Host("cue file :", $CueFullName)
$content = Get-Content -Path $CueFullName
$lineOld = $content | Select-String 'FILE "' | Select-Object -ExpandProperty Line
Write-Host("line old :", $lineOld)
$lineNew = 'FILE "' + $BinName + '" WAVE'
Write-Host("line new :", $lineNew)
# $FinalCueName = $CueFullName # rewrite original CUE file
$FinalCueName = $CueTest # create new test CUE file
# $content -replace $lineOld, $lineNew | Set-Content -Encoding utf8NoBOM -Path $FinalCueName -Force # problem with parentheses in PowerShell
# https://stackoverflow.com/questions/39101291/replace-does-not-replace-string-with
# $content -replace [Regex]::Escape($lineOld), $lineNew | Set-Content -Encoding utf8NoBOM -Path $FinalCueName -Force # OK
# https://stackoverflow.com/questions/17144355/how-can-i-replace-every-occurrence-of-a-string-in-a-file-with-powershell
# don't require PowerShell 6+, this method take care of the encoding of the file (UTF-8 without BOM)
$content2 = [System.IO.File]::ReadAllText($CueFullName).Replace($lineOld, $lineNew)
[System.IO.File]::WriteAllText($FinalCueName, $content2)
Write-Host("")
}
# 2022-05-12
#Requires -Version 6.0
Get-ChildItem -File -Recurse -Include "*.srt" | ForEach-Object {
$FileFullName = $_.FullName
Write-Host("file :", $FileFullName)
$FileTest = [io.path]::ChangeExtension($FileFullName, 'test.srt') # test
$content = Get-Content -Path $FileFullName
# $content | Set-Content -Encoding utf8NoBOM -Path $FileFullName -Force # rewrite original SRT file
$content | Set-Content -Encoding utf8NoBOM -Path $FileTest -Force # create new test SRT file
Write-Host("")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment