Develop/Windows
[인코딩] UTF-8 with BOM to UTF-8 without BOM
크레이지J
2025. 5. 30. 14:55
반응형
윈도우에서 UTF-8 with BOM 파일을 찾아 모두 UTF-8 without BOM 형식으로 변환하기.
아래에서 첫 번째에 최상위 경로를 주고 powershell에서 실행하면 일괄로 변경한다.
$targetFolder = "data" # 변환할 최상위 폴더 경로
# UTF-8 BOM 3바이트 시퀀스
$utf8BOM = [byte[]](0xEF,0xBB,0xBF)
# 대상 폴더 내 모든 파일 재귀 탐색
Get-ChildItem -Path $targetFolder -File -Recurse | ForEach-Object {
$file = $_.FullName
# 파일을 바이트 단위로 읽기
$bytes = [System.IO.File]::ReadAllBytes($file)
# BOM 존재 여부 체크
if ($bytes.Length -ge 3 -and $bytes[0] -eq $utf8BOM[0] -and $bytes[1] -eq $utf8BOM[1] -and $bytes[2] -eq $utf8BOM[2]) {
Write-Host "BOM 발견 및 제거 중: $file"
# BOM 제거한 바이트 배열 생성
$bytesWithoutBOM = $bytes[3..($bytes.Length - 1)]
# BOM 제거된 바이트를 임시 파일에 기록
$tempFile = [System.IO.Path]::GetTempFileName()
[System.IO.File]::WriteAllBytes($tempFile, $bytesWithoutBOM)
# 임시 파일을 원본 파일로 덮어쓰기
Move-Item -Path $tempFile -Destination $file -Force
}
else {
Write-Host "BOM 없음: $file"
}
}
이 시스템에서 스크립트를 실행할 수 없으므로 ~~~파일을 로드할 수 없습니다. 라고 에러가 나면.
관리자 모드 powershell에서
Set-ExecutionPolicy RemoteSigned