Solved

How to define and launch command line items

Posted on 2003-10-27
14
226 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
Comment Utility
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
Comment Utility
But how do I define a string that takes the command line item?
0
 
LVL 9

Accepted Solution

by:
Dang123 earned 20 total points
Comment Utility
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
 
LVL 1

Author Comment

by:Krelek200
Comment Utility
I'll give it a shot
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
You should just need to put the line as the user would type it after your program name. What trouble are you having?

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks you all.  I have got things working the way it should
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now