Solved

How to define and launch command line items

Posted on 2003-10-27
14
229 Views
Last Modified: 2010-05-03
Hi,

     I am wondering how I can launch items in a command line setting.  I am currently setting up a viewer for an application but I want to be able to launch the application with a command line parameter.  IE application.exe FILE.DAT

     Anyway to do this in VB 6.0?

Thx
Krelek
0
Comment
Question by:Krelek200
  • 7
  • 3
  • 2
  • +1
14 Comments
 
LVL 11

Expert Comment

by:Steiner
ID: 9627293
Have a look at the Command-Function. To try it out, use the following code:

Private Sub Form_Activate()
  MsgBox Command  'This should show the parameters your app was called with
End Sub
0
 
LVL 1

Author Comment

by:Krelek200
ID: 9627334
But how do I define a string that takes the command line item?
0
 
LVL 9

Accepted Solution

by:
Dang123 earned 20 total points
ID: 9627569
You would assign the string returned from the Command function to a string variable in your program. You would then need to parse the string.

Based on your question you could use

Dim strFile as String


strFile = Command
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:Krelek200
ID: 9627583
I'll give it a shot
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 50 total points
ID: 9627902
Just a couple quick comments:

You may not know that you can pass Command Line Parameters to your app in the IDE.  Select Project, then Properties and then switch to the Make tab.  There is a spot there to place your Command Line Parameters for testing.

This is the typical algorithm used to process command line arguments:
'---------------------------------------------------------------------------
Dim commandLine As String
Dim parameters As Variant
Dim a As Integer
   
commandLine = Command()
parameters = Split(commandLine) ' Splits using " " by default
For a = LBound(parameters) To UBound(parameters)
  Debug.Print "Parameter " & a & " = " & parameters(a)
Next a
'---------------------------------------------------------------------------

Some pitfalls I have seen with command line arguments:
If there are extra spaces between the parameters (which could occur if the program is called from the DOS prompt and somebody is typing in the command line), the split function will give each space its own place in the array.  You can eliminate these kind of problems by checking the length of each parameter like so:
'---------------------------------------------------------------------------
Dim commandLine As String
Dim parameters As Variant
Dim a As Integer, b As Integer
   
b = 0
commandLine = Command()
parameters = Split(commandLine) ' Splits using " " by default
For a = LBound(parameters) To UBound(parameters)
    If Len(Trim(parameters(a))) > 0 Then
        b = b + 1
        Debug.Print "Parameter " & b & " = " & parameters(a)
    End If
Next a
'---------------------------------------------------------------------------

Also, if you call your viewer programmatically from another app, it is possible that you will pass a full path name to the file to be viewed.  If so, you may have spaces in the path leading up to your file name.  Obviously, when you parse the command line, the path/filename will get split into two different parts.  To avoid this, always convert a fully qualified path to use short filenames, thus removing any spaces.

Hope this helps out.  Happy Coding...
0
 
LVL 1

Author Comment

by:Krelek200
ID: 9628863
What do you put in the command line parameters to enable them.  I am having some trouble with the Make tab about the parameters.
Krelek
0
 
LVL 9

Expert Comment

by:Dang123
ID: 9628912
You should just need to put the line as the user would type it after your program name. What trouble are you having?

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9628984
It is the second box from the bottom on the make tab labeled "Command Line Arguments:"

If you were to put "planets.jpg shuttle.jpg /slideshow" in the box, then this would be the equivalent of running your app from the command line like this:

yourAppName.exe planets.jpg shuttle.jpg /slideshow

That's it.
0
 
LVL 1

Author Comment

by:Krelek200
ID: 9629360
It works inside of Visual Basic but when I create the EXE file it states that it cannot load one of its components.  Do you have to specify the like this:

C:\direct\direct\myAppName.exe C:\direct\direct\fileToLoad.Ext

Thanks
Krelek
0
 
LVL 1

Author Comment

by:Krelek200
ID: 9629441
I understand the comment about converting a fully qualified path to use short filenames.  Now how do I handle these long strings that is created?

Thx
Krelek
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9629641
Does the error occur during the compile in the IDE or when you run the .exe directly?

What are you using to create the short filenames, what are your storing them in, and where do you want them to go?
0
 
LVL 1

Author Comment

by:Krelek200
ID: 9630112
I have solved the error problem.  I am storing them in a subdirectory that is short (8) letters long.  I believe that is waht you meant by short filenames.

Thx
Krelek
0
 
LVL 11

Assisted Solution

by:Steiner
Steiner earned 20 total points
ID: 9631788
Just my 2cents: If you only want exactly 1 parameter for your app (eg. 1 file with full path), then you can forget about splitting the command-string and also about long and short file names. Just take the string as it comes.
I guess the main problem about the long filenames is, that these may contain spaces which are then used to split the string because your app thinks that beyond the space comes a new argument.
0
 
LVL 1

Author Comment

by:Krelek200
ID: 9635837
Thanks you all.  I have got things working the way it should
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB 6.0 printer how to align 6 67
Using "ScreenUpdating" 6 73
Recommendation vb6 to vb.net or others 14 175
Send outlook email from VBS Script 2 42
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…

839 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