?
Solved

How to define and launch command line items

Posted on 2003-10-27
14
Medium Priority
?
232 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
[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
  • 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
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.

 
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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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

764 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