Search code examples
delphidelphi-7

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


Solution

  • 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;