Results 1 to 3 of 3

Thread: Help: Cmd Line Parse

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Location
    Home
    Posts
    85

    Help: Cmd Line Parse

    OK I am looking for an API Function that can hopefully parse a command line and file path. If no API Exists to do this, does anyone have any simple functions to perform the task:

    Example:
    Code:
    Dim sPath as String: sPath = "C:\Documents and Settings\something.exe -a -t -k -v"
    
    Dim sFile as String: sFile = GetFile(sPath)
    Dim sCmd as String: sCmd = GetCmd(sPath)

    Making the following true:
    sFile = "C:\Documents and Settings\something.exe"
    sCmd = "-a -t -k -v"

    So I need some file and command line parser either in WinAPI or some custom made function.
    Anyone care to share a solution or idea. Feel Free. This has been bugging me for months.

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Help: Cmd Line Parse

    vb Code:
    1. Dim sPath As String
    2. Dim intStart As Integer
    3.  
    4. sPath = "C:\Documents and Settings\something.exe -a -t -k -v"
    5.  
    6. intStart = InStrRev(sPath, ".exe") + 4
    7.  
    8. MsgBox VBA.Left$(sPath, intStart)
    9. MsgBox Mid$(sPath, intStart)

  3. #3
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Help: Cmd Line Parse

    The proper format to have that line should be:
    Code:
    """C:\Documents and Settings\something.exe"" -a -t -k -v"
    Thus resulting into a string: "C:\Documents and Settings\something.exe -a -t -k -v

    Windows always adds quotes around a filepath if it contains spaces. This is also true for parameters passed to the program. Now this allows us to use a QuoteSplit:
    Code:
    Public Function QuoteSplit(ByRef Expression As String, Optional ByRef Delimiter As String = " ", Optional ByVal Compare As VbCompareMethod = vbBinaryCompare) As String()
        Dim sParts() As String, N As Long
        sParts = Split(Expression, """")
        For N = 0 To UBound(sParts) Step 2
            sParts(N) = Replace(sParts(N), Delimiter, Delimiter & vbNullChar, , , Compare)
        Next N
        QuoteSplit = Split(Join(sParts, """"), Delimiter & vbNullChar)
    End Function
    And an usage example:
    Code:
        Dim Parameters() As String
    
        Parameters = QuoteSplit("""C:\Documents and Settings\something.exe"" -a -t -k -v")
    
        ' now Parameters contains the following items:
        Parameters(0) = """C:\Documents and Settings\something.exe"""
        Parameters(1) = "-a"
        Parameters(2) = "-t"
        Parameters(3) = "-k"
        Parameters(4) = "-v"
    And this should make your life easier

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width