Simple Fortune Script in vbs for wsh

Just been learning some scripting with MS Visual Basic (VBS) and the Windows Scripting Host (wscript), and wanted to recreate the Unix Fortune command in Windows to process a .txt file, such as below, and spit out a random line in a message box.

Born to be sold...
The Cheese plant has been stolen.
Palace of the Gods
A biological gestalt device.
A quantum entity.
It mush have been love.
Crash! Boom! Bang!

And here is the script, nice and simple.  Assuming you save the txt below to  Fortune.vbs and have an input file in the format as above, called input.txt, you would run from a command line or shortcut as “wscript Fortune.vbs input.txt”

Set fs = CreateObject("Scripting.FileSystemObject")
Set txt = fs.OpenTextFile(WScript.Arguments(0))
Sections = Split(txt.ReadAll, vbCrLf & "%" & vbCrLf)
Randomize Timer
Temp=MsgBox(Sections(Round(Rnd * UBound(Sections))),0,"Today's Fortune")

Which would produce the output:
fortuneYou could put it in Windows Startup, or in the registry as the following command:

C:\Windows\System32\wscript.exe c:\fortune\fortune.vbs input.txt

Leave a Reply