Home All Groups Group Topic Archive Search About
Author
6 Jul 2009 6:37 AM
Bee
I am working a VB6 app that needs to properly parse the command line input.
It may be coming in from a shortcut startup or otherwise.
Is there an API that will properly parse the Command$ input?
I have tried a few routines if found but they not not do it properly.
I guess I also need to know the rules too.
It seems that sometimes a path may or may not have double quotes around it.
Seems to depend on if it is by itself or if there are additional arguments.
Double quotes needed only when spaces are present in the path and additional
arguments or arguments with spaces.

Author
6 Jul 2009 7:18 AM
Randem
Parsing is not that complicated. Here are the basics:

1 - Scan the line for the existance of double quotes

Line without quotes...

1 - Use the Split function to separate the line by spaces into an array.

2 - Use replace on each array element to replace the double quotes with null

Line with quotes...

1 - Use the Split function to separate the line by double quotes into an
array.

2 - Use Split function to separate by space on each element except the
second array element. The second element is will be the one in which the
quotes were surrounding. IE. your path...

Of course if your line has more than two quotes you will need to do a manual
operation to separate out the entries.

1 - Cycle thru the line moving each charater into a holding string break if
any of your separator characters are found. IE. Space, Double Quote etc...

2 - If you find a quote you will need to set a boolean to true so that you
know you need to find a matching quote and ignore the space separator.

3 - Move holding string into an array element.


If you post your code, I can take a look to see where you have gone awry...

--
Randem Systems
Your Installation Specialist
The Top Inno Setup Script Generator
http://www.randem.com/innoscript.html
Disk Read Error Press Ctl+Alt+Del to Restart
http://www.randem.com/discus/messages/9402/9406.html?1236319938



Show quoteHide quote
"Bee" <B**@discussions.microsoft.com> wrote in message
news:A85E7621-28CA-4F6F-B592-BB3AB63C1975@microsoft.com...
>I am working a VB6 app that needs to properly parse the command line input.
> It may be coming in from a shortcut startup or otherwise.
> Is there an API that will properly parse the Command$ input?
> I have tried a few routines if found but they not not do it properly.
> I guess I also need to know the rules too.
> It seems that sometimes a path may or may not have double quotes around
> it.
> Seems to depend on if it is by itself or if there are additional
> arguments.
> Double quotes needed only when spaces are present in the path and
> additional
> arguments or arguments with spaces.
>
>
>
Author
6 Jul 2009 2:37 PM
Nobody
Author
6 Jul 2009 3:17 PM
Nobody
Here is my pure VB solution, which I have tested. It supports double quotes
and it even has a function to extract the path part from an argument.
Arguments can be separated with one or more white spaces(tabs, spaces),
unless they are part of a double quote string. Doubling of double quotes are
not supported, however, this is not a problem with paths. Double quotes are
optional around paths with no spaces. To try the code, set the start up
object to Sub Main, and try the code below. Here are two test cases and the
output for each:

Case 1:

/Q /Debug /Path=C:\Test\Test1 /test

Output:

/Q
/Debug
/Path=C:\Test\Test1
Path is 'C:\Test\Test1'
/test


Case 2:

/Q    /Debug /Path=""C:\Program Files"" /Test

Output:

/Q
/Debug
/Path="C:\Program Files"
Path is 'C:\Program Files'
/Test



' Module 1 ================

Option Explicit

Public Sub Main()
On Error GoTo Main_Error
    Dim Args() As String
    Dim Argc As Long
    Dim i As Long
    Dim sCommandLine As String

    sCommandLine = Trim(Command())
    'sCommandLine = "/Q /Debug /Path=C:\Test\Test1 /test"
    'sCommandLine = "/Q    /Debug /Path=""C:\Program Files"" /Test"
    Args = GetCommandLineArgs(sCommandLine)
    On Error Resume Next
    Argc = UBound(Args)
    Err.Clear
    On Error GoTo Main_Error

    For i = 1 To Argc
        Debug.Print Args(i)
        If StrComp(Args(i), "/debug", vbTextCompare) = 0 Then
            'bDebug = True
        End If
        If StrComp(Left(Args(i), 6), "/path=", vbTextCompare) = 0 Then
            ' Path found
            ' 6 = Len("/path=")
            Debug.Print "Path is '" & GetPathFromArg(Right(Args(i), _
                Len(Args(i)) - 6)) & "'"
        End If
    Next

ExitSub:
    Exit Sub
Main_Error:
    MsgBox "Main:" & Erl & vbCrLf & "Error " & Err.Number & ": " & _
        Err.Description
    Resume ExitSub
End Sub

' Returns the command line split into arguments, or an ampty array if
' there is an error. Spaces in the middle of quotes are considered part
' of the same argument
Public Function GetCommandLineArgs(ByRef sCommand As String) As String()
    Dim Args() As String
    Dim Argc As Long
    Dim i As Long
    Dim InQuote As Boolean
    Dim ch As String
    Dim chAsc As Long
    Dim sCurrentArg As String

    On Error GoTo GetCommandLineArgs_Error

    For i = 1 To Len(sCommand)
        ch = Mid(sCommand, i, 1)
        chAsc = Asc(ch)
        Select Case chAsc
            Case 9, 32:
                If InQuote Then
                    sCurrentArg = sCurrentArg & ch
                Else
                    ' Argument ended, or extra spaces at the begining
                    If Len(sCurrentArg) > 0 Then
                        Argc = Argc + 1
                        ReDim Preserve Args(1 To Argc) As String
                        Args(Argc) = sCurrentArg
                        sCurrentArg = ""
                    Else
                        ' Ignore it
                    End If
                End If
            Case 34: ' Outlook uses Chr(148) in HTML email mode
                If InQuote Then
                    ' End quote found
                    InQuote = False
                Else
                    ' Start quote found
                    InQuote = True
                End If
                sCurrentArg = sCurrentArg & ch
            Case Else
                sCurrentArg = sCurrentArg & ch
        End Select
    Next

    If Len(sCurrentArg) > 0 Then
        ' Process last fargment
        Argc = Argc + 1
        ReDim Preserve Args(1 To Argc) As String
        Args(Argc) = sCurrentArg
    End If

    GetCommandLineArgs = Args

ExitSub:
    Exit Function
GetCommandLineArgs_Error:
    MsgBox "GetCommandLineArgs:" & Erl & vbCrLf & "Error " & Err.Number & _
        ": " & Err.Description
    Resume ExitSub
End Function


' Returns the path part of an argument, for example "C:\Program Files"
' without the quotes when /path="C:\Program Files" is specified, or the
' entire argument if no double qoutes were found. Empty string if no path
' is speciefied or an error occured
Public Function GetPathFromArg(ByRef sArg As String) As String
    Dim posQuoteStart As Long
    Dim posQuoteEnd As Long

    On Error GoTo GetPathFromArg_Error

    posQuoteStart = InStr(1, sArg, Chr(34), vbBinaryCompare)
    If Len(sArg) > posQuoteStart Then
        posQuoteEnd = InStr(posQuoteStart + 1, sArg, Chr(34), _
            vbBinaryCompare)
        If posQuoteEnd = 0 Then
            posQuoteEnd = Len(sArg) + 1
        End If
        GetPathFromArg = Mid(sArg, posQuoteStart + 1, _
            posQuoteEnd - posQuoteStart - 1)
    End If

ExitSub:
    Exit Function
GetPathFromArg_Error:
    MsgBox "GetPathFromArg:" & Erl & vbCrLf & "Error " & Err.Number & _
        ": " & Err.Description
    Resume ExitSub
End Function
Author
6 Jul 2009 4:16 PM
Bee
Thank you.  I will step through these and see what is going on.


Show quoteHide quote
"Bee" wrote:

> I am working a VB6 app that needs to properly parse the command line input.
> It may be coming in from a shortcut startup or otherwise.
> Is there an API that will properly parse the Command$ input?
> I have tried a few routines if found but they not not do it properly.
> I guess I also need to know the rules too.
> It seems that sometimes a path may or may not have double quotes around it.
> Seems to depend on if it is by itself or if there are additional arguments.
> Double quotes needed only when spaces are present in the path and additional
> arguments or arguments with spaces.
>
>
>