?
Solved

How to define and launch command line items

Posted on 2003-10-27
14
Medium Priority
?
237 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 80 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
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 1

Author Comment

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

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 200 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 86

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 86

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 80 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

850 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