| |
| |
| |
| |
| |
| |
| |
| |
| $htstr = @{ |
| '111' = '222' |
| 'aaa' = 'bbb' |
| } |
| |
| $rootPath = ".\*" |
| function Get-Encoding { |
| [CmdletBinding(DefaultParameterSetName = "PathSet")] |
| param ( |
| [Parameter(ParameterSetName = "StreamSet", Mandatory = $true)] |
| [ValidateNotNullOrEmpty()] |
| [System.IO.Stream]$Stream, |
| [Parameter(ParameterSetName = "PathSet", Mandatory = $true, Position = 0)] |
| [ValidateNotNullOrEmpty()] |
| [System.String]$Path, |
| [Parameter(Mandatory = $false, Position = 1)] |
| [System.UInt32]$ReadCount = 1024 |
| ) |
| $utf8BOMThrow = New-Object System.Text.UTF8Encoding -ArgumentList @($true, $true) |
| $utf8NoBOMThrow = New-Object System.Text.UTF8Encoding -ArgumentList @($false, $true) |
| $utf16LEBOMThrow = New-Object System.Text.UnicodeEncoding -ArgumentList @($false, $true, $true) |
| $utf16LENoBOMThrow = New-Object System.Text.UnicodeEncoding -ArgumentList @($false, $false, $true) |
| $utf16BEBOMThrow = New-Object System.Text.UnicodeEncoding -ArgumentList @($true, $true, $true) |
| $utf16BENoBOMThrow = New-Object System.Text.UnicodeEncoding -ArgumentList @($true, $false, $true) |
| |
| $arrUTF8Bom = $utf8BOMThrow.GetPreamble() |
| $arrUTF16LEBom = $utf16LEBOMThrow.GetPreamble() |
| $arrUTF16BEBom = $utf16BEBOMThrow.GetPreamble() |
| |
| if ($PSCmdlet.ParameterSetName -eq "PathSet") { |
| try { |
| $Stream = New-Object System.IO.FileStream -ArgumentList @($Path, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read) |
| } catch { |
| return $null |
| } |
| } |
| $byteBuff = New-Object byte[] -ArgumentList 3 |
| $readCount = $Stream.Read($byteBuff, 0, 3) |
| if ($byteBuff[0] -eq $arrUTF8Bom[0] -and $byteBuff[1] -eq $arrUTF8Bom[1] -and $byteBuff[2] -eq $arrUTF8Bom[2]) { |
| |
| $return = $utf8BOMThrow |
| } elseif ($byteBuff[0] -eq $arrUTF16LEBom[0] -and $byteBuff[1] -eq $arrUTF16LEBom[1]) { |
| |
| $return = $utf16LEBOMThrow |
| } elseif ($byteBuff[0] -eq $arrUTF16BEBom[0] -and $byteBuff[1] -eq $arrUTF16BEBom[1]) { |
| |
| $return = $utf16BEBOMThrow |
| } else { |
| |
| if ($ReadCount -gt 0) { |
| $charBuff = New-Object char[] -ArgumentList $ReadCount |
| } |
| |
| foreach ($encoding in @($utf8NoBOMThrow)) { |
| try { |
| $Stream.Position = 0 |
| $sr = New-Object System.IO.StreamReader -ArgumentList @($Stream, $encoding, $false) |
| if ($ReadCount -gt 0) { |
| [void]$sr.Read($charBuff, 0, $ReadCount) |
| } else { |
| [void]$sr.ReadToEnd() |
| } |
| $return = $encoding |
| break |
| } catch { |
| |
| } finally { |
| if ($sr) { |
| $sr.Dispose() |
| } |
| } |
| } |
| } |
| if ($PSCmdlet.ParameterSetName -eq "PathSet") { |
| $Stream.Dispose() |
| } |
| if (!$return) { |
| $return = [System.Text.Encoding]::Default |
| } |
| return $return |
| } |
| Get-ChildItem -Path $rootPath -Recurse | ForEach-Object { |
| if (-not $_.PSIsContainer -and $_.Name -like '*.json' -or $_.Name -like '*.xml') { |
| try { |
| Write-Host $_.FullName |
| $encoding = Get-Encoding -Path $_.FullName |
| $txt = [System.IO.File]::ReadAllText($_.FullName, $encoding) |
| foreach ($key in $htstr.Keys) { |
| $txt = $txt.Replace($key, $htstr[$key]) |
| } |
| [System.IO.File]::WriteAllText($_.FullName, $txt, $encoding) |
| } finally { |
| |
| } |
| trap {} |
| } |
| $newName = $_.Name |
| foreach ($key in $htstr.Keys) { |
| $newName = $newName.Replace($key, $htstr[$key]) |
| } |
| if ($newName -ne $_.Name) { |
| Rename-Item -LiteralPath $_.FullName -NewName ($newName ) -Force -Verbose -ErrorAction SilentlyContinue |
| } |
| }COPY |