Re: How can I make a Delphi program with COMMAND LINE options.

Posted by webmaster Guido on October 18, 2002

In Reply to: How can I make a Delphi program with COMMAND LINE options. posted by QWERTYX85 on October 13, 2002

: How can I make a program that have some COMMAND LINE options.
: For example when I write in command line " c:\MyProg /? "How can I make
: my program write in other options dos mode like " c:\command /? ". But I ask it
: both console apps or windows apps. I hope I can explain.

The Delphi function ParamCount returns the number of parameters passed to the program on the command line.
The function ParamStr(Index) returns the parameter from the command line that corresponds to Index, or an empty string if Index is greater than ParamCount. For example, an Index value of 2 returns the second command-line parameter.

Here is a Delphi code example that reads all the command-line parameters, shows in a label how many there are, and displays all the parameters in a ListBox, one per line:

procedure TForm1.FormCreate(Sender: TObject);
  i: integer;
  lblParamCount.Caption := IntToStr(ParamCount);
  if ParamCount > 0 then
    for i := 1 to ParamCount do

For "post-MSDOS" readers, who never (or almost never) saw an MSDOS- or other character-based session, here's a quick refresher (or history lesson ;-)
The term "command line" originated in pre-Windows times, it is what you type at the famous "prompt". For example, to start an application under MSDOS, type its filename (you may omit the extension .exe or .com) and press ENTER.

Under Windows, you basically can enter a command-line in two ways:
1. open an MSDOS-screen, type the command and press ENTER;
2. or type it in the "Run" dialog and click OK (accessed through Windows' Startmenu).

"Command line parameters" are the extra information that can follow a command. The parameters are separated from the command and from each other by spaces. For example, the following command will start WinZip.exe with two parameters:
WinZip MyArchive *.*
WinZip reads the parameters "MyArchive" and "*.*" and next it compresses all the files of the current directory into a file with the name

For specifying command line parameters that include spaces, such as long filenames, you use double quotes, e.g.:
MyProg "c:\My Documents\Delphi Tutorial"

Related Articles and Replies:

Find related articles

Suggested keywords:
  command line,  pass parameters,  application parameters,  shellexecute


[ DelphiLand Discussion Forum ]