PoshCode Archive  Artifact [ab6692f786]

Artifact ab6692f7860712a2fc5fde1bd82510611407fe180c43041e46f85eb5bfb7d526:

  • File Get-Tail.ps1 — part of check-in [5a36207184] at 2018-06-10 13:13:15 on branch trunk — Tails a File, but does not work. No output to screen, not even an error when not setting number of lines variables. (user: William Stacey size: 1413)

# encoding: ascii
# api: powershell
# title: Get-Tail.ps1
# version: 0.1
# type: script
# license: CC0
# function: Get-Tail
# x-poshcode-id: 2618
# x-archived: 2011-04-20T17:29:46
# x-published: 2011-04-18T23:56:00
#
# Name: Get-Tail.ps1
# Author: William Stacey
# Created: 02/22/2007
# Description: Gets the last N lines of a file. Does scan from end-of-file so works on large files. Also has a loop flag that prompts for refresh.
 
function Get-Tail([string]$path = $(throw "Path name must be specified."), [int]$count = 10, [bool]$loop = $false)
{
	if ( $count -lt 1 ) {$(throw "Count must be greater than 1.")}
	function get-last
	{
		$lineCount = 0
		$reader = new-object -typename System.IO.StreamReader -argumentlist $path, $true
		[long]$pos = $reader.BaseStream.Length - 1
 
		while($pos -gt 0)
		{
			$reader.BaseStream.Position = $pos
			if ($reader.BaseStream.ReadByte() -eq 10)
			{
				if($pos -eq $reader.BaseStream.Length - 1)
				{
					$count++
				}
				$lineCount++
				if ($lineCount -ge $count) { break }
			}
			$pos--
		} 
		
		if ($lineCount -lt $count)
		{
			$reader.BaseStream.Position = 0
		}
		
		while($line = $reader.ReadLine())
		{
			$lines += ,$line
		}
		
		$reader.Close()
		$lines
	}
 
	while(1)
	{
		get-last
		if ( ! $loop ) { break }
		$in = read-host -prompt "Hit [Enter] to tail again or Ctrl-C to exit"
	}
}