Listing 1: Sorter.vbs Option Explicit ' BEGIN COMMENT ' Define the script constants. ' END COMMENT Const SCRIPT_NAME = "Sorter.vbs" ' BEGIN COMMENT ' Define the Win32 API constants. ' END COMMENT Const ERROR_FILE_NOT_FOUND = 2 Const ERROR_ALREADY_EXISTS = 183 Main() Sub Usage() WScript.Echo "Sorts an input file, or standard input, and writes it to an output file or" _ & vbNewLine & "standard output." & vbNewLine & vbNewLine _ & "Usage: " & SCRIPT_NAME & " [] [/O:] [/D] [/I] [/R]" _ & vbNewLine & vbNewLine _ & " is the input file to sort. If not specified, uses standard input." _ & vbNewLine & vbNewLine _ & " is the output file. If not specified, uses standard output." _ & vbNewLine & vbNewLine _ & "/D performs a descending (Z-A and 9-0) sort." & vbNewLine & vbNewLine _ & "/I ignores case when sorting." & vbNewLine & vbNewLine _ & "/R performs a random sort (lines are rearranged in random order)." WScript.Quit End Sub ' BEGIN COMMENT ' End the script with an error message and exit code. ' END COMMENT Sub Die(ByVal ErrorMessage, ByVal ReturnCode) WScript.Echo ErrorMessage WScript.Quit ReturnCode End Sub ' BEGIN COMMENT ' Return the script host executable. ' END COMMENT Function ScriptHost() ScriptHost = LCase(Mid(WScript.FullName, Len(WScript.Path) + 2)) End Function Sub Main() Dim Args, VBSort, Result, FSO, FN, TSInput, TSOutput, Arr, N Set Args = WScript.Arguments If Args.Named.Exists("?") Then Usage() If ScriptHost() <> "cscript.exe" Then _ Die "You must run this script with the CScript host.", 1 On Error Resume Next Set VBSort = CreateObject("Penton.VBSort") ' BEGIN COMMENT ' Cache the Number property of the Err object. ' END COMMENT Result = Err On Error GoTo 0 ' BEGIN CALLOUT A If Result <> 0 Then _ Die "Unable to create the Penton.VBSort object.", Result ' END CALLOUT A Set FSO = CreateObject("Scripting.FileSystemObject") If Args.Unnamed.Count > 0 Then FN = Args.Unnamed(0) If Not FSO.FileExists(FN) Then Die "File not found - " & FN, ERROR_FILE_NOT_FOUND Else On Error Resume Next Set TSInput = FSO.OpenTextFile(FN) If Err Then _ Die "Error 0x" & Hex(Err) & " opening input file", Err On Error GoTo 0 End If Else Set TSInput = WScript.StdIn End If If Args.Named("O") <> "" Then FN = Args.Named("O") If FSO.FileExists(FN) Then Die "File already exists - " & FN, ERROR_ALREADY_EXISTS Else On Error Resume Next Set TSOutput = FSO.OpenTextFile(FN, 2, True) If Err Then TSInput.Close Die "Error 0x" & Hex(Err) & " opening output file", Err End If On Error GoTo 0 End If Else Set TSOutput = WScript.StdOut End If ' BEGIN COMMENT ' Set the Descending, IgnoreCase, and Random properties if the /d, ' /i, and /r options, respectively, exist on the command line. ' END COMMENT VBSort.Descending = Args.Named.Exists("D") VBSort.IgnoreCase = Args.Named.Exists("I") VBSort.Random = Args.Named.Exists("R") ' BEGIN CALLOUT B On Error Resume Next Arr = Split(TSInput.ReadAll, vbNewLine) ' BEGIN COMMENT ' Cache the Number property of the Err object. ' END COMMENT Result = Err On Error GoTo 0 ' END CALLOUT B ' BEGIN CALLOUT C If Result = 0 Then ' BEGIN COMMENT ' Call the component's Sort method. ' END COMMENT VBSort.Sort Arr For N = 0 To UBound(Arr) TSOutput.WriteLine Arr(N) Next End If ' END CALLOUT C TSInput.Close TSOutput.Close End Sub