views:

82

answers:

1

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

+3  A: 

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;
Andreas Rejbrand
Thanks for your help but there is a little problem, when i use this:ShellExecute(Handle, nil, 'cmd.exe', '/K cd C:\WINDOWS', nil, SW_SHOWNORMAL);cmd will execute but not at my entered path, it starts from where i saved my project.
Armin
I invite you to read the [documentation about ShellExecute](http://msdn.microsoft.com/en-us/library/bb762153.aspx), Armin. There you'll find out about what the fifth parameter is for.
Rob Kennedy
I don't know what was the matter but after some tries it works properly. thank you, it was useful help.
Armin