Chris Warwick, @cjwarwickps, July 2017
Read a Quote/Cookie/Fortune/MOTD from a file and display it.
The fortunes file consists of thousands of random quotes. As a quote can contain newline characters, each individual quote is delimited with a 0x00 character; the delimiter is also present as the first and last character in the file in order to simplify the parsing below.
Rather than reading the entire file before selecting a random quote the script uses the FileStream.Seek method to locate and read a randomly-placed small chunk of the file. A quotes is then selected randomly and extracted from the resutling data.
Bugs: There's no error checking. The quote file must be ASCII encoded.
Historical Note: The quote file used here is just over 600kB in size. Back in the day this could have take some considerable time to read from (slow) disk. Now, of course, with devices having GBs of memory and SSDs, this size is trivial and the effort of attempting to read the file efficiently as opposed to simply grabbing the whole thing with Get-Content is largely academic. However, the techniques may still be of value if truly large files are being processed...
<#
.Synopsis
This function displays a randomly chosen quote from a quote file
.Description
Displays a random quote/cookie/fortune/message-of-the-day
.Example
Get-Quote
I've had a perfectly wonderful evening. But this wasn't it.
-- Groucho Marx
.Parameter QuoteFile
A file containing quotes; will use '.\fortunes.dat' by default.
#>
V1.5 (Current Version) July 2017
Fixed bug with quote file path; Updated comments.
V1.3 - 1.4 (Dev Versions) July 2017
Just messin' with VSCode & GitHub.
V1.2 November 2015
Added alias; tidied tags
V1.1 November 2015
Update to Readme
V1.0
Initial release to the PowerShell Gallery
V0.1-0.9
Dev versions
See all my other PS Gallery modules:
Find-Module | Where Author -like 'Chris*Warwick'