← ClaudeAtlas

fix-file-encodinglisted

偵測並修正檔案亂碼問題,依副檔名轉換至正確目標編碼(Big5/ANSI → UTF-8 系列)。
CloudyWing/ai-dotfiles · ★ 0 · Code & Development · score 75
Install: claude install-skill CloudyWing/ai-dotfiles
# 修正檔案編碼 ## 使用方式 ``` /fix-file-encoding [檔案路徑或 glob 模式] ``` 若未傳入路徑,詢問使用者要處理哪些檔案。 ## 執行步驟 ### 1. 確認目標檔案 列出符合條件的檔案清單,讓使用者確認範圍後再繼續。 ### 2. 判斷目標編碼 依副檔名與專案框架決定目標編碼,規則同 CLAUDE.md §2 Encoding Strategy: | 檔案類型 | 目標編碼 | | --- | --- | | `*.ps1` | UTF-8 **with BOM** | | `*.csv` | UTF-8 **with BOM** | | `.NET Framework` 的 `*.cs`, `*.vb`, `*.aspx`, `*.master` | UTF-8 **with BOM** | | `net4x` 框架的 `*.cshtml` | UTF-8 **with BOM** | | 其他所有檔��� | UTF-8 **無 BOM** | 判斷 `*.cshtml` 的框架版本:讀取同專案的 `.csproj`,檢查 `<TargetFramework>` 欄位。`net4x` 為 .NET Framework,其餘(`net5+`、`netcoreapp`)為 .NET Core/ASP.NET Core。 ### 3. 偵測來源編碼 使用 PowerShell 偵��檔案的實際編碼: ```powershell [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 $bytes = [System.IO.File]::ReadAllBytes("$FilePath") if ($bytes.Length -ge 3 -and $bytes[0] -eq 0xEF -and $bytes[1] -eq 0xBB -and $bytes[2] -eq 0xBF) { "UTF-8 with BOM" } elseif ($bytes.Length -ge 2 -and $bytes[0] -eq 0xFF -and $bytes[1] -eq 0xFE) { "UTF-16 LE" } else { "Unknown (likely ANSI/Big5)" } ``` 若偵測為「Unknown」,嘗試以 Big5 解碼,若解碼後中文可讀,則來源編碼視為 Big5。 ### 4. 轉換編碼 若來源編碼與目標編碼相同,告知使用者無需轉換並跳過。 執行轉換(PowerShell): ```powershell [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 $content = [System.IO.File]::ReadAllText("$FilePath", [System.Text.Encoding]::GetEncoding(950)) # Big5 # 目標為 UTF-8 無 BOM(不可用 [System.Text.Encoding]::UTF8,該屬性會寫出 BOM) [System.IO.File]::WriteAllText("$OutputPath", $content, (New-Object System.Text.UTF8Encoding($false))) # 目標為 UTF-8 with