-
Notifications
You must be signed in to change notification settings - Fork 95
/
Get-Lines.ps1
36 lines (30 loc) · 884 Bytes
/
Get-Lines.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# https://www.reddit.com/r/PowerShell/comments/4payc9/confirm_file_length_and_format_before_loading/
# this should work, but why not use get-content?
# get-content -readlines 1000
# see 'streamwriter example...'
# !
# this
# $stream = New-Object System.IO.StreamReader -ArgumentList $file
# is the same type as this!
# $stream = [System.IO.File]::OpenText($file)
function Get-Lines ([string]$file) {
begin {
$file = (Resolve-Path $file).ToString()
if (!(Test-Path $file)) {
Throw "File not found: $file"
}
try {
#$stream = New-Object System.IO.StreamReader $file
$stream = [System.IO.File]::OpenText($file)
} catch {
Throw $_
}
}
process {
while (!$stream.EndOfStream) {
$stream.ReadLine()
}
$stream.Close()
rv stream
}
}