Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Parameter To CMD

How can i send parameters to CMD? for example send a path and start it from that path? How can i execute CMD commands? Thanks

like image 542
Armin Taghavizad Avatar asked Aug 18 '10 19:08

Armin Taghavizad


People also ask

How do you define a parameter from the command line?

This means that the first argument the user enters on the command line is automatically inserted for this parameter. If you want to define a named parameter, for which the user must specify the parameter name from the command line, leave the Position keyword out of the attribute declaration.

How do I add a parameter to a cmdlet?

The basic syntax for declaring this attribute is [Parameter ()]. A parameter must be explicitly marked as public. Parameters that are not marked as public default to internal and are not found by the Windows PowerShell runtime. This cmdlet uses an array of strings for the Name parameter.

How do I get the value of a parameter in batch?

Batch parameters (Command line parameters): In the batch script, you can get the value of any argument using a % followed by its numerical position on the command line. The first item passed is always %1 the second item is always %2 and so on.

What is a cmdlet parameter in PowerShell?

A cmdlet parameter enables the user to provide input to the cmdlet. In the following example, Get-Proc and Get-Member are the names of pipelined cmdlets, and MemberType is a parameter for the Get-Member cmdlet. The parameter has the argument "property."


2 Answers

To start cmd.exe and immediately execute a command, use the /K flag:

procedure TForm1.FormCreate(Sender: TObject);
begin
  ShellExecute(Handle, nil, 'cmd.exe', '/K cd C:\WINDOWS', nil, SW_SHOWNORMAL);
end;

To run a command in cmd.exe and then immediately close the console window, use the /C flag:

procedure TForm1.FormCreate(Sender: TObject);
begin
  ShellExecute(Handle, nil, 'cmd.exe', '/C del myfile.txt', nil, SW_SHOWNORMAL);
end;
like image 94
Andreas Rejbrand Avatar answered Sep 19 '22 02:09

Andreas Rejbrand


You can also use the Process class - see an example below

AProcess := TProcess.Create(nil); // Create process
AProcess.Executable := 'cmd';                             // Executable to run
AProcess.Parameters.Add('/T:B0');                         // Set background colour
AProcess.Parameters.Add('/K');                            // Keep open

AProcess.Parameters.Add('title');                         // A title for cmd
AProcess.Parameters.Add('My Console');                    // Title
AProcess.Parameters.Add('&&');                            // Start a new command line
AProcess.Parameters.Add('cd');                            // Change directory
AProcess.Parameters.Add('D:\X\');                        // Path to Folder

 {Set environment variable}
AProcess.Parameters.Add('&&');                            // Start a new command line
AProcess.Parameters.Add('HOME='+MYSQL_DIR);                // Set env example

AProcess.Parameters.Add('&&');                            // Start a new command line
AProcess.Parameters.Add('mysql.exe');                     // run mysql.exe
AProcess.Parameters.Add('--host=' + VAR_HOST);          // Parameter server
AProcess.Parameters.Add('--port=' + VAR_PORT); // Parameter mysql server port

AProcess.Execute; // execute detatched process command window remains visible
AProcess.Free;    // free memory        
like image 32
PodTech.io Avatar answered Sep 20 '22 02:09

PodTech.io