{"id":3092,"date":"2019-03-12T16:38:45","date_gmt":"2019-03-12T15:38:45","guid":{"rendered":"https:\/\/www.mariotti.de\/?p=3092"},"modified":"2019-03-12T16:38:45","modified_gmt":"2019-03-12T15:38:45","slug":"zeilenendezeichen-und-codepage-in-dateien-via-powershell","status":"publish","type":"post","link":"https:\/\/dev.mariotti.de\/?p=3092","title":{"rendered":"Zeilenendezeichen und Codepage in Dateien via PowerShell"},"content":{"rendered":"<p>Vor kurzem musste ich bei einer Vielzahl von Skript- und Konfigurationsdateien sicherstellen, dass sie einheitlich das in der Windows Welt \u00fcbliche CRLF als Zeilenendezeichen (EOL)\u00a0 benutzen, und nicht CR wie unter MacOS oder LF wie unter Linux. Au\u00dferdem musste ich scherstellen, dass alle Dateien ASCII kodiert waren und zwar incl. der passenden Codepage f\u00fcr Westeuropa, damit auch Deutsche Umlaute richtig dargestellt werden. Hier ist mein PowerShell Skript mit dem ich dies bewerkstellig habe.<\/p>\n<pre><code class=\"language-powershell\"> \nFunction Set-EOL {\n    [CmdletBinding()]\n    Param(\n        [Parameter(Mandatory=$True,Position=1)]\n        [string]$File,\n        [Parameter(Mandatory=$True,Position=2)]\n        [ValidateSet(\"mac\",\"unix\",\"win\")] \n        [string]$LineEnding,\n        [Parameter(Mandatory=$True,Position=3)]\n        [string]$Encoding\n    )\n        Switch ($LineEnding) {\n            \"mac\"  { $eol=\"`r\" }\n            \"unix\" { $eol=\"`n\" }\n            \"win\"  { $eol=\"`r`n\" }\n        } \n        [System.Text.Encoding]$encoding = [System.Text.Encoding]::GetEncoding($Encoding)\n        $text = [IO.File]::ReadAllText($file,$encoding) -replace \"`r`n\", \"`n\"  # Replace CR+LF with LF\n        [IO.File]::WriteAllText($file, $text,$encoding)  # Replace CR with LF\n        $text = [IO.File]::ReadAllText($file,$encoding) -replace \"`r\", \"`n\"\n        [IO.File]::WriteAllText($file, $text,$encoding)\n        #  Now line-endings are LF, replace now LF with intended EOL char\n        if ($eol -ne \"`n\") {\n            $text = [IO.File]::ReadAllText($file,$encoding) -replace \"`n\", $eol\n            [System.IO.File]::WriteAllText($file,$text,$encoding)\n        }\n}\n\nGet-ChildItem -Path \"C:\\Temp\\*\" -Include *.txt,*.ps1,*.json -Recurse -Depth 3 | ForEach-Object {\n    Set-EOL -File $_.FullName -LineEnding 'win' -Encoding 'iso-8859-1'\n}<\/code><\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PowerShell Skript zum \u00e4ndern der Zeilenendezeichen und der Codepage bei Datein<\/p>\n","protected":false},"author":2,"featured_media":2425,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[],"class_list":["post-3092","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"_links":{"self":[{"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/posts\/3092","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3092"}],"version-history":[{"count":0,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/posts\/3092\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/media\/2425"}],"wp:attachment":[{"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}