Skip to content

Instantly share code, notes, and snippets.

@luthermonson
Last active June 10, 2020 21:26
Show Gist options
  • Save luthermonson/e489775f4edf729cc3b7ac473480205d to your computer and use it in GitHub Desktop.
Save luthermonson/e489775f4edf729cc3b7ac473480205d to your computer and use it in GitHub Desktop.
Powershell Native crlf and lf Functions
<#
switch between crlf and lf much like unix2dos and dos2unix but implemented in powershell.
put two functions into your $PROFILE and call like the following:
default params is your current working dir and ignoring .git and vendor dirs
crlf file.txt
crlf ./dir @(".git", "vendor")
lf file.txt
lf ./dir @(".git", "vendor")
#>
function crlf([string]$path=$pwd, [string[]]$exclude=@(".git", "vendor")) {
foreach ($item in Get-ChildItem $path) {
if ($exclude | Where {$item -like $_}) { continue }
if (Test-Path $item.FullName -PathType Container) {
$item.FullName
crlf $item.FullName $exclude
} else {
$item.FullName
(Get-Content -Path $item.FullName) | Set-Content $item.FullName
}
}
}
function lf([string]$path=$pwd, [string[]]$exclude=@(".git", "vendor")) {
foreach ($item in Get-ChildItem $path) {
if ($exclude | Where {$item -like $_}) { continue }
if (Test-Path $item.FullName -PathType Container) {
$item.FullName
lf $item.FullName $exclude
} else {
$item.FullName
((Get-Content -Path $item.FullName) -Join "`n") + "`n" | Set-Content $item.FullName -NoNewline
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment