4

I have a batch file and an application developed in delphi7. Both are working fine. I want to run the batch file when clicking a button. what can I do for that?

Thanks Nelson

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Nelson T Joseph
  • 2,683
  • 8
  • 39
  • 56

2 Answers2

8

Use ShellExecute which is declared in ShellAPI.

ShellExecute(
    MainForm.Handle,
    'open',
    PChar(scriptfilename),
    PChar(params),
    PChar(workingdirectory),
    SW_SHOW
);
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • @Nelson Does this answer the question? – David Heffernan Jun 17 '11 at 08:53
  • @DavidHeffernan Can we use CreateProcess instead of SE? how? – saastn Aug 24 '15 at 15:10
  • To use `CreateProcess` you'd have to send it `cmd.exe` as the executable, and then whatever arguments are needed to run the script. Typically that would be `/C scriptfilename.bat` – David Heffernan Aug 24 '15 at 15:12
  • @DavidHeffernan if you mean passing `'cmd.exe /C scriptfilename.bat'` as 'lpCommandLine' I've already tried it.. I'm using [RunDosInMemo](http://stackoverflow.com/questions/9119999/getting-output-from-a-shell-dos-app-into-a-delphi-app/9120103#9120103), it only shows an empty cmd window, and my app stops on `until (Apprunning <> WAIT_TIMEOUT)` loop till I close cmd manually, however the command runs OK from Run window. should I change anything in RunDosInMemo? – saastn Aug 24 '15 at 15:45
  • @saastn I can't do this in comments to an unrelated question. If you've got a question about the code you have, ask it. – David Heffernan Aug 24 '15 at 15:46
0
 var
 exe_start_map:string;
 begin
  exe_start_map:=(ExtractFileDir(Application.ExeName));     
   winexec(PChar(exe_start_map+'\yourfile.bat'),sw_show);
 end;

you must add ShellApi in uses list