57 lines
1.4 KiB
PowerShell
57 lines
1.4 KiB
PowerShell
|
function Test-FileEOF {
|
||
|
<#
|
||
|
.SYNOPSIS
|
||
|
Returns false if EOF isn't an empty line.
|
||
|
|
||
|
.DESCRIPTION
|
||
|
Test the given file against the EOF standard (final empty/blank line + CRLF) and returns true or false.
|
||
|
|
||
|
.PARAMETER Path
|
||
|
Relative or full path to an existing file.
|
||
|
|
||
|
.INPUTS
|
||
|
[none]
|
||
|
|
||
|
.OUTPUTS
|
||
|
[bool]
|
||
|
|
||
|
.EXAMPLE
|
||
|
Test-FileEOF -Path './testfile.txt'
|
||
|
|
||
|
.NOTES
|
||
|
#>
|
||
|
|
||
|
[CmdletBinding()]
|
||
|
[OutputType([bool])]
|
||
|
|
||
|
param (
|
||
|
[Parameter(Mandatory = $true)]
|
||
|
[string]$Path
|
||
|
)
|
||
|
|
||
|
begin {
|
||
|
}
|
||
|
|
||
|
process {
|
||
|
if (-not (Test-FileEOL -Path $Path)) {
|
||
|
Write-Warning -Message ('The given file does not use CRLF! ({0})' -f $Path)
|
||
|
Write-Output $false
|
||
|
}
|
||
|
$content = Get-Content -Path $Path -Raw -Encoding 'utf8'
|
||
|
$lastLine = ($content -split "`r`n")[-1].Length
|
||
|
# Test for multiple lines without content on EOF
|
||
|
$perLine = ($content -split "`r`n")[-2].Length
|
||
|
if (($lastLine -eq 0) -and ($perLine -ne 0)) {
|
||
|
Write-Debug -Message ('EOF: LastLine {0}; PenultimateLine {1} -> true' -f $lastLine, $perLine)
|
||
|
Write-Output $true
|
||
|
}
|
||
|
else {
|
||
|
Write-Debug -Message ('EOF: LastLine {0}; PenultimateLine {1} -> false' -f $lastLine, $perLine)
|
||
|
Write-Output $false
|
||
|
}
|
||
|
}
|
||
|
|
||
|
end {
|
||
|
}
|
||
|
}
|