Created
January 10, 2012 19:01
-
-
Save brunomlopes/1590535 to your computer and use it in GitHub Desktop.
Powershell function to get the endline format of a file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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