by Syed Natiq Abbas Kazmi (3 Submissions)
Category: Miscellaneous
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Sun 27th July 2003
Date Added: Mon 8th February 2021
Rating: (1 Votes)
How to implement command-line parameters in Visual Basic
Function GetCommandLine(Optional NumberOfArguments)
•-- Declare variables.
Dim C, CmdLine, CmdLnLen, InArg, I, ArgNum
•-- See if NumberOfArguments was provided.
If IsMissing(NumberOfArguments) Then
NumberOfArguments = 10
End If
•-- Make array of the correct size.
ReDim ArgArray(NumberOfArguments)
ArgNum = 0
InArg = False
•-- Get command line arguments.
CmdLine = Command()
CmdLnLen = Len(CmdLine)
•-- Go thru command line one character
•-- at a time.
For I = 1 To CmdLnLen
C = Mid(CmdLine, I, 1)
•-- Test for space or tab.
If (C <> " " And C <> vbTab) Then
•-- Neither space nor tab.
•-- Test if already in argument.
If Not InArg Then
•-- New argument begins.
•-- Test for too many arguments.
If ArgNum = NumberOfArguments Then Exit For
ArgNum = ArgNum + 1
InArg = True
End If
•-- Concatenate character to current argument.
ArgArray(ArgNum) = ArgArray(ArgNum) & C
Else
•-- Found a space or tab.
•-- Set InArg flag to False.
InArg = False
End If
End If
Next I
•-- Resize array just enough to hold arguments.
ReDim Preserve ArgArray(ArgNum)
•-- Return Array in Function name.
GetCommandLine = ArgArray()
End Function