Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to extract information from Command$ String

Posted on 1999-09-09
14
Medium Priority
?
194 Views
Last Modified: 2010-05-18
A Shell call made in my application, the Command$ is composed of ExeName,AppName and InstallName. Do you know how to extract these information respectively?
0
Comment
Question by:Jun080298
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 4
  • +2
14 Comments
 
LVL 32

Expert Comment

by:Erick37
ID: 2035304
From Books Online

This example uses the Command function to get the command line arguments in a function that returns them in a Variant containing an array.
Function GetCommandLine(Optional MaxArgs)
      'Declare variables.
      Dim C, CmdLine, CmdLnLen, InArg, I, NumArgs
      'See if MaxArgs was provided.
      If IsMissing(MaxArgs) Then MaxArgs = 10
      'Make array of the correct size.
      ReDim ArgArray(MaxArgs)
      NumArgs = 0: InArg = False
      'Get command line arguments.
      CmdLine = Command()
      CmdLnLen = Len(CmdLine)
      'Go thru command line one character
      'at a time.
      For I = 1 To CmdLnLen
            C = Mid(CmdLine, I, 1)
            'Test for space or tab.
            If (C <> " " And C <> vbTab) Then
                  'Neither space nor tab.
                  'Test if already in argument.
                  If Not InArg Then
                  'New argument begins.
                  'Test for too many arguments.
                        If NumArgs = MaxArgs Then Exit For
                        NumArgs = NumArgs + 1
                        InArg = True
                  End If
                  'Concatenate character to current argument.
                  ArgArray(NumArgs) = ArgArray(NumArgs) & C
            Else
                  'Found a space or tab.
                  'Set InArg flag to False.
                  InArg = False
            End If
      Next I
      'Resize array just enough to hold arguments.
      ReDim Preserve ArgArray(NumArgs)
      'Return Array in Function name.
      GetCommandLine = ArgArray()
End Function
0
 
LVL 2

Expert Comment

by:ggilman
ID: 2035311
Not sure if this is exactly what you are looking for but it might work. GetCommandLine will return 1 element from the command line. It's not really optimized right now but you should get the idea. It was adapted from Books Online.


Private Function GetCommandLine(ArgNum As Long) As String
    'Declare variables.
    Dim C, CmdLine, CmdLnLen, InArg, I, NumArgs
    Dim maxArgs As Long
   
    maxArgs = ArgNum
    'Make array of the correct size.
    ReDim ArgArray(maxArgs)
    NumArgs = 0: InArg = False
    'Get command line arguments.
    CmdLine = Command()
    CmdLnLen = Len(CmdLine)
    'Go thru command line one character
    'at a time.
    For I = 1 To CmdLnLen
        C = Mid(CmdLine, I, 1)

'Test for space or tab.
        If (C <> " " And C <> vbTab) Then
            'Neither space nor tab.
            'Test if already in argument.
            If Not InArg Then
            'New argument begins.
            'Test for too many arguments.
                If NumArgs = maxArgs Then Exit For
                NumArgs = NumArgs + 1
                InArg = True
            End If
            'Concatenate character to current argument.
            ArgArray(NumArgs) = ArgArray(NumArgs) & C
        Else
            'Found a space or tab.

'Set InArg flag to False.
            InArg = False
        End If
    Next I
    'Resize array just enough to hold arguments.
    ReDim Preserve ArgArray(NumArgs)
    'Return Array in Function name.
    GetCommandLine = ArgArray(ArgNum)
End Function
0
 
LVL 18

Expert Comment

by:mdougan
ID: 2035317
Ouch, Erick37 beat me to it... ;)

The following function GetCommandLine was pulled from the Visual Basic Help file.  You could call this in your VB app like so:

Dim ExeName as String
Dim AppName as String
Dim InstallName as String
Dim vArray as variant

vArray = GetCommandLine(3)
ExeName = vArray(1)
AppName = vArray(2)
InstallName = vArray(3)


Function GetCommandLine(Optional MaxArgs)
      'Declare variables.
      Dim C, CmdLine, CmdLnLen, InArg, I, NumArgs
      'See if MaxArgs was provided.
      If IsMissing(MaxArgs) Then MaxArgs = 10
      'Make array of the correct size.
      ReDim ArgArray(MaxArgs)
      NumArgs = 0: InArg = False
      'Get command line arguments.
      CmdLine = Command()
      CmdLnLen = Len(CmdLine)
      'Go thru command line one character
      'at a time.
      For I = 1 To CmdLnLen
            C = Mid(CmdLine, I, 1)

'Test for space or tab.
            If (C <> " " And C <> vbTab) Then
                  'Neither space nor tab.
                  'Test if already in argument.
                  If Not InArg Then
                  'New argument begins.
                  'Test for too many arguments.
                        If NumArgs = MaxArgs Then Exit For
                        NumArgs = NumArgs + 1
                        InArg = True
                  End If
                  'Concatenate character to current argument.
                  ArgArray(NumArgs) = ArgArray(NumArgs) & C
            Else
                  'Found a space or tab.

'Set InArg flag to False.
                  InArg = False
            End If
      Next I
      'Resize array just enough to hold arguments.
      ReDim Preserve ArgArray(NumArgs)
      'Return Array in Function name.
      GetCommandLine = ArgArray()
End Function

Now, this is the explanation based on understanding your question to mean that your application was called by another application using the Shell command and passing command line parms.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:mdougan
ID: 2035323
So, Jun, I think the lesson to be learned here is that there is lots of good info available right at your finger tips in either the VB Help file or Books OnLine. :)
0
 

Author Comment

by:Jun080298
ID: 2036712

Hi Erick37',

Thank you for your help. Since I think Mdougan's
amswer is more closer my question, I decide to give
him this 100 points, and I would like to give you
50 points. Is it OK?
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2037093
That's OK, but if you are referring to the code, it is identical.  Cut'n'pasted from Books Online.  ggilman's code is the same too, but at least it is indented.

Good luck!
0
 
LVL 18

Expert Comment

by:mdougan
ID: 2037149
I think Jun means the example code I provided for calling the function.  However, I don't know that this is worth the extra 50 pts.  Erick37 (and ggilman) was faster on the button than I, so, I have no problem if Erick gets the points.  But I'll leave that up to Jun, just tell us who you want to answer for credit.
0
 

Author Comment

by:Jun080298
ID: 2037300
Both of you did a great job. So this 100 points for Mdougan,
and Erick37 takes extra 50 points I have posted for him.
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2037318
mdougan:
You were just being thorough, that's worth something.
Jun:
Please delete the Q "Points for Erick37"
0
 

Author Comment

by:Jun080298
ID: 2037362
Hi Mdougan,

What is the "For"?
 'New argument begins.
 'Test for too many arguments.
 If NumArgs = MaxArgs Then Exit For  <---
 NumArgs = NumArgs + 1
 InArg = True
 End If
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2037388
From Books Online ;)

Exiting a Control Structure
The Exit statement allows you to exit directly from a For loop, Do loop, Sub procedure, or Function procedure. The syntax for the Exit statement is simple: Exit For can appear as many times as needed inside a For loop, and Exit Do can appear as many times as needed inside a Do loop:
For counter = start To end [Step increment]
      [statementblock]
      [Exit For]
      [statementblock]
Next [counter[, counter] [,...]]
      
Do [{While | Until} condition]
      [statementblock]
      [Exit Do]
      [statementblock]
Loop

0
 

Author Comment

by:Jun080298
ID: 2037537
Thank you! Please lock the question.
0
 
LVL 18

Accepted Solution

by:
mdougan earned 400 total points
ID: 2037542
Thanks!

And yes, Erick37 is correct, the code that says Exit For is a way of breaking out of the For/Next loop that starts with the statement:

For I = 1 To CmdLnLen

In this case, if you passed in the Optional parameter of the maximum number of arguments you were interested in (in my example I passed in a number of 3) then, for efficiency sake, it gets out of the For/Next loop after it has retrieved all 3 parameters.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6852239
This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects the problem and the expert will now receive these points; points verified.

Please click on your Member Profile and select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.  If you are an EE Pro user, you can also choose Power Search to find all your open questions.

This is the Community Support link, if help is needed, along with the link to All Topics which reflects many TAs recently added.

http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
http://www.experts-exchange.com/jsp/zonesAll.jsp
 
Thank you,
Moondancer
Moderator @ Experts Exchange
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question