PowerShell 脚本

检查、替换换行符

param(
  [string]$path = "."
)
$textExtensions = @(".css", ".html", ".js", ".json", ".md", ".py", ".ts", ".txt", ".xml")
Get-ChildItem -Path $path -Recurse -File | Where-Object {
  $textExtensions -contains $_.Extension.ToLower()
} | ForEach-Object {
  $file = $_.FullName
  $lines = Get-Content $file -Raw -ErrorAction SilentlyContinue
  if (-not $lines) { return }
  if ($lines -match "`r`n") {
    Write-Host "[CRLF] " $file -ForegroundColor Red
  } elseif ($lines -match "`n") {
    Write-Host "[LF]   " $file -ForegroundColor Green
  } else {
    Write-Host "[Unknown] " $file
  }
}
param(
  [string]$path = "."
)
$textExtensions = @(".css", ".html", ".js", ".json", ".md", ".py", ".ts", ".txt", ".xml")
Get-ChildItem -Path $path -Recurse -File | Where-Object {
  $textExtensions -contains $_.Extension.ToLower()
} | ForEach-Object {
  $file = $_.FullName
  try {
    $content = Get-Content $file -Raw -ErrorAction Stop
  } catch {
    Write-Host "无法读取文件: $file" -ForegroundColor Yellow
    return
  }
  if ($content -match "`r`n") {
    $newContent = $content -replace "`r`n", "`n"
    try {
      $newContent | Out-File $file -Encoding utf8 -NoNewline
      Write-Host "替换成功: $file" -ForegroundColor Green
    } catch {
      Write-Host "替换失败: $file" -ForegroundColor Red
    }
  }
}