0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windows PowerShellで文字コードを一括変換(BOM付き/SJIS → UTF-8 BOMなし)

Posted at

Javaファイルの文字コードが UTF-8(BOM付き)、UTF-8(BOMなし)、Shift_JIS が混在している状態になっていたので、それらをすべて UTF-8(BOMなし)に一括変換したい

Powershell

$folder = "C:\Your\Target\Directory"  # ←変換対象のパスに変更!

$extensions = @("*.java", "*.xml", "*.properties")

# UTF-8かどうか判定する関数
function IsUTF8([byte[]]$bytes) {
    try {
        [System.Text.Encoding]::UTF8.GetString($bytes) | Out-Null
        return $true
    } catch {
        return $false
    }
}

foreach ($ext in $extensions) {
    Get-ChildItem -Path $folder -Recurse -Filter $ext | ForEach-Object {
        $path = $_.FullName
        $bytes = [System.IO.File]::ReadAllBytes($path)

        $encoding = New-Object System.Text.UTF8Encoding $false  # UTF-8 (BOMなし)
        $text = ""

        if ($bytes.Length -ge 3 -and $bytes[0] -eq 0xEF -and $bytes[1] -eq 0xBB -and $bytes[2] -eq 0xBF) {
            $text = [System.Text.Encoding]::UTF8.GetString($bytes, 3, $bytes.Length - 3)
            Write-Host "[UTF-8 with BOM] → $path"
        }
        elseif (IsUTF8 $bytes) {
            $text = [System.Text.Encoding]::UTF8.GetString($bytes)
            Write-Host "[UTF-8 no BOM] → $path"
        }
        else {
            $sjis = [System.Text.Encoding]::GetEncoding("shift_jis")
            $text = $sjis.GetString($bytes)
            Write-Host "[Shift_JIS] → $path"
        }

        [System.IO.File]::WriteAllText($path, $text, $encoding)
    }
}
eclipse {
    classpath {
        plusConfigurations += [configurations.testRuntimeClasspath]

        file {
            whenMerged { classpath ->
                classpath.entries.each { entry ->
                    if (entry instanceof org.gradle.plugins.ide.eclipse.model.ProjectDependency) {
                        entry.entryAttributes.remove("without_test_code")
                    }
                }
            }
        }
    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?