by Nik Keso (1 Submission)
Category: String Manipulation
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (2 Votes)
This function returns an array with the command line arguments,
contained in the command$, like cmd.exe does (with %1 , %2 ...)
If the function does not succeed, the returned array has Ubound=-1 , like split function in VB.
Inputs
INPUT: comSTR = the string with the arguments (command$)
Assumes
Just copy and paste the code in your project.
Code Returns
OUTPUT: Array of string with the arguments
'===================================================================
' GetCommandArgs - © Nik Keso 2009
'----------------------------------
'The function returns an array with the command line arguments,
'contained in the command$, like cmd.exe does (with %1 , %2 ...)
'If the function does not succeed, the returned array has Ubound=-1 ,
'like split function in VB.
'----------------------------------
'INPUT: comSTR = the string with the arguments (command$)
'OUTPUT: Array of string with the arguments
'===================================================================
Function GetCommandArgs(ByVal comSTR As String) As String()
Dim CountQ As Integer 'chr(34) counter
Dim OpenQ As Boolean ' left open string indicator (ex: "c:\bbb ccc.bat ) OpenQ=true, (ex: "c:\bbb ccc.bat" ) OpenQ=false
Dim ArgIndex As Integer
Dim tmpSTR As String
Dim strIndx As Integer
Dim TmpArr() As String
GetCommandArgs = Split("", " ") 'trick to return uninitialized array like split, if function does NOT succeed!
TmpArr = Split("", " ")
comSTR = Trim$(comSTR) 'remove front and back spaces
If Len(comSTR) = 0 Then Exit Function
CountQ = UBound(Split(comSTR, """"))
If CountQ Mod 2 = 1 Then Exit Function 'like cmd.exe , command$ must contain even number of chr(34)=(")
strIndx = 1
Do
If Mid$(comSTR, strIndx, 1) = """" Then OpenQ = Not OpenQ
If Mid$(comSTR, strIndx, 1) = " " And OpenQ = False Then
If tmpSTR <> "" Then 'don't include the spaces between args as args!!!!!
ReDim Preserve TmpArr(ArgIndex)
TmpArr(ArgIndex) = tmpSTR
ArgIndex = ArgIndex + 1
End If
tmpSTR = ""
Else
tmpSTR = tmpSTR & Mid$(comSTR, strIndx, 1)
End If
strIndx = strIndx + 1
Loop Until strIndx = Len(comSTR) + 1
ReDim Preserve TmpArr(ArgIndex)
TmpArr(ArgIndex) = tmpSTR
GetCommandArgs = TmpArr
End Function