'************************************************************************* '* Quip.Vbs by Leslie Houk '* '* SYNTAX: [cscript|wscript] quip.vbs [] [/?|-?|/h|/help|-h|-help|--help] '* '* DESCRIPTION: Reads a text file of one-line quips and randomly displays '* one of them in a Popup box. '* '* PARAMETERS: : Specifies the input file of quips. '* '* /?,etc.: Displays this help file. '************************************************************************* Option Explicit Dim objWSH, objFileSys, objFile, intQuip, intIndex Dim strQuipFile, strQuip, strLineIn, strArgument ReDim aryTemp( 0 ) Const READONLY = 1 Set objFileSys = CreateObject( "Scripting.FileSystemObject" ) Set objWSH = CreateObject( "Wscript.Shell" ) strQuipFile = "D:\sys\quip.dat" For Each strArgument In WScript.Arguments if strArgument = "/?" Or strArgument = "-?" Or _ LCase( strArgument ) = "/h" Or LCase( strArgument ) = "-h" Or _ LCase( strArgument ) = "/help" Or LCase( strArgument ) = "-help" Or _ LCase( strArgument ) = "--help" Then objWSH.Popup _ "Quip.Vbs Version 1.0 by Leslie Houk" & vbCrLf & vbCrLf & _ "SYNTAX: [cscript|wscript] quip.vbs [] " & _ "[/?|-?|/h|/help|-h|-help|--help]" & vbCrLf & vbCrLf & _ "DESCRIPTION: Reads a text file of one-line quips and " & _ "randomly displays" & vbCrLf & _ " one of them in a Popup box." & vbCrLf & vbCrLf & _ "PARAMETERS: : Specifies the input file of quips. " & _ "The default is " & vbCrLf & " " & _ Chr( 34 ) & strQuipFile & Chr( 34 ) & vbCrLf & vbCrLf & _ " /?, -?, /h, etc.: Displays this help file." Wscript.Quit( 3 ) Else strQuipFile = strArgument End If Next '*************************************************************************** '* This algorithm is taken from the O'Reilly book "Programming perl" by '* Larry Wall and Randal L. Schwartz, Chapter 5: Common Tasks With Perl, '* Section: Selecting A Random Line From A File. Quoting from the book: '* '* "This procedure selects a line at random from a file, using '* just one pass over the file and without knowing in advance '* the number of lines. It works by calculating the probability '* that the current line (indicated by the $. variable) would '* be selected if this line were the last line in the file. The '* first line is selected with a probability of 100%, but the '* second line has a 50% change of replacing the first one, the '* third line a 33% chance of replacing one of the first two, '* and so on." '*************************************************************************** Randomize If objFileSys.FileExists( strQuipFile ) Then Set objFile = objFileSys.OpenTextFile( strQuipFile, READONLY ) If Err.Number <> 0 Then objWSH.Popup " Error " & Err.Number & ": " & Err.Description Err.Clear End If intIndex = 1 Do Until objFile.AtEndOfStream strLineIn = objFile.ReadLine If ( Rnd * intIndex ) < 1.0 Then intQuip = intIndex strQuip = strLineIn End If intIndex = intIndex + 1 Loop objFile.Close Set objFile = Nothing Else objWSH.Popup " WARNING! " & strQuipFile & " can NOT be read!" End If objWSH.Popup "Quip #" & FormatNumber( intQuip, 0, 0, -1 ) & ":" & _ vbCrLf & vbCrLf & strQuip, 0, "Quip.vbs"