When you run a program from a command line (like in "Command Prompt", or the "Start" -> "Run.." dialog, or in the properties of a shortcut), you often find that the program allows you to type something extra after the program name, which affects what the program does when it starts. For example, a command line of notepad.exe c:\file.txt would start NotePad, and automatically Open the text file c:\file.txt

To allow a VB program to do the same, you can simply use the Command function, which returns a String containing everything after your program name (in my example for NotePad, it would be "c:\file.txt"), and you can use this string in any way you like.

For example, if you just want to display what was entered, you could do this:
Code:
Dim strCommandLine as String
  strCommandLine = Command
  MsgBox strCommandLine
If you compile your program as Project1.Exe , and at the command line you enter Project1.exe hello , the string (and so the message box) will contain "hello"

The usual place to put this code is in Form_Load or Sub Main (whichever runs when your program starts).


Note that you don't have to compile your program to test the code, as you can enter command line arguments on the "Make" tab of the "Project" -> "Properties" dialog, eg:

Name:  command line.gif
Views: 31520
Size:  23.3 KB

What I have written so far is actually everything you need to know about reading the command line parameters, anything else you want is just a matter of working with a String (as you normally would), and perhaps a minor re-organisation of your code to allow you to use it as you want to. However, as further steps are usually asked too, I will give some general advice.


Working with a single parameter (perhaps a file name)
If you want to do something similar to my NotePad example (specify a file name, and the program Opens that file), you just need to call your code which opens a file, and pass the string (which contains the file name) to it.

If your current code to open a file does extra work too (like asking the user to specify the file), just move the code that does the actual opening to a seperate Sub or Function, and call it with the file name as a parameter.

For example, if your current code is like this:
Code:
Private Sub mnuOpen_Click
Dim strFileName as String
  '(code here to show a common dialog)
  strFileName = CommonDialog1.FileName

  Open strFileName For Input ...
  ...


End Sub
..it could be converted to this:
Code:
Private Sub mnuOpen_Click
Dim strFileName as String
  '(code here to show a common dialog)
  strFileName = CommonDialog1.FileName
  
  MyOpenRoutine strFileName 

End Sub

Private Sub MyOpenRoutine(p_strFileName as String)

  Open p_strFileName For Input ...
  ...


End Sub
It is a good idea to make sure at the start of MyOpenRoutine that the file name actually specifies a valid file, which you can do with the Dir function.

With the code now split into separate routines, your startup code can be changed to this:
Code:
Dim strCommandLine as String
  strCommandLine = Command
  If strCommandLine <> "" Then  'only try to open a file if one was specified!
    MyOpenRoutine strCommandLine
  End If
Working with multiple parameters
Several programs allow you to specify more than one parameter, and usually each one is started with a different character (either / or - ) so that the program can tell them apart.

If you want to do this, you can use the Split function to put the parameters into an array, eg:
Code:
Dim strParameters() As String
  strParameters = Split(Command, "/")

'for this example, just show each item
Dim intLoop as Integer
  For intLoop = LBound(strParameters) to UBound(strParameters)
    MsgBox strParameters(intLoop)
  Next intLoop
What you would actually do with the array for your program depends on the parameters you want to accept, and what you want them to do - there are so many possibilities that it isn't worth going into them here!