Search Tools Links Login

Get an Array of string from Command$


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.

Original Author: Nik Keso

Inputs

INPUT: comSTR = the string with the arguments (command$)

Assumptions

Just copy and paste the code in your project.

Returns

OUTPUT: Array of string with the arguments

Side Effects

none

API Declarations

none

Code

'===================================================================
' 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:bb ccc.bat ) OpenQ=true, (ex:  "c:bb 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

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 111 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.