Skip to content

Instantly share code, notes, and snippets.

@orange-in-space
Last active May 17, 2025 02:53
Show Gist options
  • Select an option

  • Save orange-in-space/42f10e17ec02506117c58b9c90dcdbfc to your computer and use it in GitHub Desktop.

Select an option

Save orange-in-space/42f10e17ec02506117c58b9c90dcdbfc to your computer and use it in GitHub Desktop.
改行コードをCR+LFにするPowerShellスクリプト><(ChatGPTが作ってくれたやつ)
# 改行コードをCR+LFにするやつ
# Generated by ChatGPT-4o. No copyright.
#
# 使い方
# .\Convert-LineEndings.ps1 "sample.txt"
#
param (
[Parameter(Mandatory = $true)]
[string]$InputFile
)
# ファイルの存在確認
if (-Not (Test-Path $InputFile)) {
Write-Error "入力ファイルが見つかりません: $InputFile"
exit 1
}
# 拡張子・ファイル名の取得
$backupFile = "$InputFile.bak"
# バックアップ作成
try {
Copy-Item -Path $InputFile -Destination $backupFile -Force
Write-Output "バックアップ作成: $backupFile"
} catch {
Write-Error "バックアップ作成中にエラーが発生しました: $_"
exit 1
}
# 改行コードの変換
try {
$content = Get-Content -Raw -Encoding UTF8 $InputFile
$normalized = $content -replace "`r?`n|`r", "`r`n"
Set-Content -Encoding UTF8 -NoNewline -Path $InputFile -Value $normalized
Write-Output "変換完了!ファイルをCR+LF形式に統一しました: $InputFile"
} catch {
Write-Error "変換中にエラーが発生しました: $_"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment