Name: Anonymous 2008-01-02 15:34
optimization (desperately) welcome
example usage;
function_copy C:\file.txt "C:\program files\out.put" -x -y
should return [function_copy, C:\file.txt, C:\program files\out.put, -x, -y]
Function CommandArguments(partstr As String) As String()
Dim args() As String
ReDim args(0)
Dim k As Integer
Dim ptr As Integer
Dim withinQuotes As Boolean
withinQuotes = False
Dim command As String
command = " " & partstr & " "
For k = 1 To Len(command)
Select Case Mid$(command, k, 1)
Case " "
If withinQuotes = False Then
ReDim Preserve args(UBound(args) + 1)
If ptr <> 0 Then args(UBound(args) - 1) = Mid$(command, ptr + 1, k - ptr - 1)
ptr = k
End If
Case Chr(34)
Select Case withinQuotes
Case False: withinQuotes = True
Case True: withinQuotes = False
End Select
End Select
Next
For k = 1 To UBound(args)
args(k) = NoQ(args(k))
args(k - 1) = args(k)
Next
ReDim Preserve args(UBound(args) - 2)
CommandArguments = args
End Function
Public Function NoQ(inst As String) As String
If Len(inst) > 0 Then
If Mid$(inst, 1, 1) = Chr(34) Then
NoQ = Mid$(inst, 2, Len(inst) - 2)
Else
NoQ = inst
End If
Else
NoQ = vbNullString
End If
End Function
example usage;
function_copy C:\file.txt "C:\program files\out.put" -x -y
should return [function_copy, C:\file.txt, C:\program files\out.put, -x, -y]
Function CommandArguments(partstr As String) As String()
Dim args() As String
ReDim args(0)
Dim k As Integer
Dim ptr As Integer
Dim withinQuotes As Boolean
withinQuotes = False
Dim command As String
command = " " & partstr & " "
For k = 1 To Len(command)
Select Case Mid$(command, k, 1)
Case " "
If withinQuotes = False Then
ReDim Preserve args(UBound(args) + 1)
If ptr <> 0 Then args(UBound(args) - 1) = Mid$(command, ptr + 1, k - ptr - 1)
ptr = k
End If
Case Chr(34)
Select Case withinQuotes
Case False: withinQuotes = True
Case True: withinQuotes = False
End Select
End Select
Next
For k = 1 To UBound(args)
args(k) = NoQ(args(k))
args(k - 1) = args(k)
Next
ReDim Preserve args(UBound(args) - 2)
CommandArguments = args
End Function
Public Function NoQ(inst As String) As String
If Len(inst) > 0 Then
If Mid$(inst, 1, 1) = Chr(34) Then
NoQ = Mid$(inst, 2, Len(inst) - 2)
Else
NoQ = inst
End If
Else
NoQ = vbNullString
End If
End Function