Unix newlines to Windows newlines (on Windows)

后端 未结 11 1849
失恋的感觉
失恋的感觉 2020-12-01 05:44

Is there a way (say PowerShell, or a tool) in Windows that can recurse over a directory and convert any Unix files to Windows files.

I\'d be perfectly happy with a wa

11条回答
  •  无人及你
    2020-12-01 05:56

    Building on @js2010 answer I've created this script

    $excludeFolders = "node_modules|dist|.vs";
    $excludeFiles = ".*\.map.*|.*\.zip|.*\.png|.*\.ps1"
    
    Function Dos2Unix {
        [CmdletBinding()]
        Param([Parameter(ValueFromPipeline)] $fileName)
    
        Write-Host -Nonewline "."
    
        $fileContents = Get-Content -raw $fileName
        $containsCrLf = $fileContents | %{$_ -match "\r\n"}
        If($containsCrLf -contains $true)
        {
            Write-Host "`r`nCleaing file: $fileName"
            set-content -Nonewline -Encoding utf8 $fileName ($fileContents -replace "`r`n","`n")
        }
    }
    
    Get-Childitem -File "." -Recurse |
    Where-Object {$_.PSParentPath -notmatch $excludeFolders} |
    Where-Object {$_.PSPath -notmatch $excludeFiles} |
    foreach { $_.PSPath | Dos2Unix }
    

提交回复
热议问题