Skip to content

Instantly share code, notes, and snippets.

@brunomlopes
Created January 10, 2012 19:01
Show Gist options
  • Save brunomlopes/1590535 to your computer and use it in GitHub Desktop.
Save brunomlopes/1590535 to your computer and use it in GitHub Desktop.
Powershell function to get the endline format of a file.
function Get-EndlineFormat {
param($filepath)
# by default get-content reads one 'item' at a time.
# and reading 1 byte at a time will take a long long time
# -ReadCount 0 makes it read it all at once.
$bytes = get-content -encoding byte -ReadCount 0 $filepath
$string = [Text.Encoding]::Ascii.GetString($bytes, 0, $bytes.Count)
$is_unix = ([regex] "[^`r]`n").Matches($string).Count -gt 0 `
-and ([regex] "`r`n").Matches($string).Count -eq 0
$is_windows = ([regex] "[^`r]`n").Matches($string).Count -eq 0 `
-and ([regex] "`r`n").Matches($string).Count -gt 0
if($is_unix -and $is_windows){
return "mixed"
}
if($is_unix){
return "unix"
}
if($is_windows){
return "windows"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment