Public Function SmartSplit(Expression As String, Delimeter As String, Optional IgnoreQuotes As Boolean, Optional AddAppPath As Boolean) As Variant
' SmartSplit function
' 2003.10.08 by korpse
'
' This function greatly enhances the built in 'Split' function of Visual Basic.
' It adds support for grouping arguments that are in quotes.
'
' This function was initially developed to split up command line arguments from Command$.
' It also supports adding the application exe path to the first element of the array.
' This will in essence make the command line arguments exactly like other programming
' languages such as Delphi's built in ParamStr() function. For example, the following:
' ParamStr() = SmartSplit(Command$ , " " , False, True)
' will return the exact same thing as Delphi's ParamStr().
'
'
' USAGE:
'
' The parameters for SmartSplit are as follows:
' Expression [string] = Text to be parsed.
' Delimeter [string] = The deliminating character. NOTE - Only supports a single character.
' IgnoreQuotes [boolean] = If False it will group arguments that are in quotes.
' AddAppPath [boolean] = If True it will add the application path to the first element of the returned array.
'
' The function returns an array of a variable size, so the variable that is assigned must
' be DIMmed as follows:
' Dim ParamStr() as string
' and then assigned like this:
' ParamStr = SmartSplit(Expression , Delimeter, False, True) 'Actual parameter will differ
'
'
' Feel free to use/post/edit this code anywhere you want, but please give me credit. Thanks!!!
On Error Resume Next
Dim i As Integer, j As Integer
Dim NoQuotes As String, InQuotes As Boolean
Dim ParsedExpression(99) As String, TrimmedParsedExpression() As String
Dim ParamNumber As Integer
'Make sure Delimeter is 1 character
If Len(Delimeter) > 1 Then Delimeter = Left$(Delimeter, 1)
If Len(Delimeter) = 0 Then Delimeter = " " 'Default to a space
If AddAppPath = True Then 'Assign first element in array to app path.
'This is useful when parsing Command$
ParsedExpression(0) = App.Path & "\" & App.EXEName & ".exe"
ParamNumber = 1
Else
ParamNumber = 0
End If
'Split Expression
For i = 1 To Len(Expression)
If InQuotes = False Then
If Mid$(Expression, i, 1) = Chr$(34) And IgnoreQuotes = False Then InQuotes = True
If Mid$(Expression, i, 1) = Delimeter Then
'one whole parameter
'make sure parameter is not blank - if it is ignore it
If Trim(ParsedExpression(ParamNumber)) <> "" Then
ParamNumber = ParamNumber + 1
ParsedExpression(ParamNumber) = ""
End If
Else
ParsedExpression(ParamNumber) = ParsedExpression(ParamNumber) & Mid$(Expression, i, 1)
End If
Else
'In quotes
If Mid$(Expression, i, 1) = Chr$(34) Then InQuotes = False
ParsedExpression(ParamNumber) = ParsedExpression(ParamNumber) & Mid$(Expression, i, 1)
End If
Next i
'Remove quotation marks if ignorequotes=false
If IgnoreQuotes = False Then
For i = 0 To UBound(ParsedExpression)
NoQuotes = ""
For j = 1 To Len(ParsedExpression(i))
If Mid$(ParsedExpression(i), j, 1) <> Chr$(34) Then NoQuotes = NoQuotes & Mid$(ParsedExpression(i), j, 1)
Next j
ParsedExpression(i) = NoQuotes
Next i
End If
'This is equivalent to a trim statement but will work with any delimeter
If ParsedExpression(ParamNumber) = "" And ParamNumber > 0 Then ParamNumber = ParamNumber - 1
'Remove blank values from end of array
TrimmedParsedExpression = ParsedExpression()
ReDim Preserve TrimmedParsedExpression(ParamNumber)
SmartSplit = TrimmedParsedExpression()
End Function