?
Solved

How to launch a .bat from an application

Posted on 2003-03-04
12
Medium Priority
?
495 Views
Last Modified: 2010-04-04
Hi,

 Does anyone know how I can call a .bat program from delphi the pause for 20 seconds and start another bat program. A Example would be great. I am using Delphi 5 if it matters.

Thanks,

ec
0
Comment
Question by:entercite
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +4
12 Comments
 
LVL 2

Expert Comment

by:cqhall
ID: 8069331
If you want each batch file to finish before starting the 20 second delay, you'll need a special call to wait for the each call to complete. See WinExecAndWait32 below.  If the timer can start immediately after the batch file starts, use WinExec, CreateProcess of ShellExecute.  WinExec is the easiest and sounds adequate for your needs.

procedure TForm1.Button1Click(Sender: TObject);
begin
 {wait for each batch file to complete - WinExecAndWait  defined below}
  WinExecAndWait32('c:\batch1.bat',sw_show);
  update; //refresh form
  sleep(20000);
  WinExecAndWait32('c:\batch2.bat',sw_show);
  update;
  sleep(20000);
  ...
          { OR }
  {timer starts immediately}
  WinExec('c:\batch1.bat',sw_show);
  update; //refresh form
  sleep(20000);
  WinExec('c:\batch2.bat',sw_show);
  update;
  sleep(20000);
  ...
end;


function WinExecAndWait32(FileName:String; Visibility : integer): DWord;

var
 zAppName     : array[0..512] of char;
 zCurDir      : array[0..255] of char;
 WorkDir      : String;
 StartupInfo  : TStartupInfo;
 ProcessInfo  : TProcessInformation;
const
 Win95=1;
 NT40=2;

begin
 StrPCopy(zAppName,FileName);
 GetDir(0,WorkDir);
 StrPCopy(zCurDir,WorkDir);
 FillChar(StartupInfo,Sizeof(StartupInfo),#0);
 StartupInfo.cb := Sizeof(StartupInfo);
 StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
 StartupInfo.wShowWindow := Visibility;
 if not CreateProcess(nil,
  zAppName,                      { pointer to command line string }
  nil,                           { pointer to process security attributes}
  nil,                           { pointer to thread security attributes }
  false,                         { handle inheritance flag }
  CREATE_NEW_CONSOLE or          { creation flags }
  NORMAL_PRIORITY_CLASS,
  nil,                           { pointer to new environment block }
  nil,                           { pointer to current directory name }
  StartupInfo,                   { pointer to STARTUPINFO }
  ProcessInfo) then Result := 0  { pointer to PROCESS_INF }
 else
  begin
   WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
   GetExitCodeProcess(ProcessInfo.hProcess,Result);
  end;
end;

0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 8069958
uses
  ShellAPI;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShellExecute(Handle,'Open','c:\MyFile1.bat',nil,nil,SW_ShowNormal);
  Sleep(20000); // wait for 20 second;
  ShellExecute(Handle,'Open','c:\MyFile2.bat',nil,nil,SW_ShowNormal);
end;
0
 
LVL 5

Expert Comment

by:Lukasz Lach
ID: 8070048
20 seconds is a long time... your app may be found as froozen, i'd rather use...


uses ShellAPI;

function BatThread(P: Pointer): LongInt; stdcall;
begin
  Form1.Button1.Enabled := False;
  ShellExecute(Handle, 'Open', 'c:\MyFile1.bat', nil, nil, SW_ShowNormal);
  Sleep(20000);
  ShellExecute(Handle, 'Open', 'c:\MyFile2.bat', nil, nil, SW_ShowNormal);
  Form1.Button1.Enabled := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  ThreadID  : DWORD;
  ThHandle  : THandle;
begin
  ThHandle := CreateThread(nil, 0, @BatThread, nil, 0, ThreadID);
  if ThHandle = 0 then MessageBox(Handle, 'Thread create error', PChar(Caption), MB_OK or MB_ICONERROR or MB_APPLMODAL);
end;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:AlexRakia
ID: 8072158
Keep it as simple as posible people !

/////////////////////////////////////
WinExec('c:\BatchFile.Bat',sw_show);
Sleep(20000);
WinExec('c:\BatchFile.Bat',sw_show);
Sleep(20000);
/////////////////////////////////////

Thanks
Alex Rakia

For questions please contact me at: AlexRakia@Yahoo.com & I'll be glad to help you.
0
 
LVL 5

Expert Comment

by:Lukasz Lach
ID: 8072304
AlexRakia does not work on NT...
0
 

Expert Comment

by:AlexRakia
ID: 8072987
AnAKin,

listen, I tested it & it runs on Win2k Server
(Win2k server is NT based as Microsoft says).
What else?
It is working very fne for me.

what is not working over there? if you have a problem then please post the problem you got with more detailes.

thanks & appreciations
alex
0
 
LVL 5

Expert Comment

by:Lukasz Lach
ID: 8073114
your code is pausing the main thread for 40 seconds. that is not a good idea for the reason of programs that wcheck programs to freeze. If you use that code user wouldn't realy like to wait 40 seconds with application no responsing and he would probably cole it from CRTL+ALT+DEL list.
In my opinion the best way is to place that if other thread so that it makes you no other problems. That is not hard IMHO.
0
 
LVL 3

Expert Comment

by:ILE
ID: 8076635
get one ontimer component

and in  the on timer event do



procedure TForm1.Timer1Timer(Sender: TObject);
begin
  WinExec('c:\BatchFile2.Bat',sw_show);            timer1.enabled:=false;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
timer1.Enabled:=false;
timer1.interval:=20000;
end;


when u like to start the first bat u do

  WinExec('c:\BatchFile1.Bat',sw_show);
  timer1.enabled:=true;




0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 8077989
Hello

  Using WinExec isn't recommended, it's available for compatibly with win 32, so you should avoid using it, and use ShellExecute or CreateProcess, for more info refer to win 32 sdk
0
 

Author Comment

by:entercite
ID: 8085479
OK,

 Sorry for the delay. I tried the code so far it does not work. Here is what I tried.

procedure TForm1.Button2Click(Sender: TObject);
begin
{wait for each batch file to complete - WinExecAndWait  defined below}
 WinExecAndWait32('C:\Program Files\Apache Tomcat 4.0\bin\shutdown.bat',sw_show);
 update; //refresh form
 WinExecAndWait32('C:\Program Files\Apache Tomcat 4.0\bin\startup.bat',sw_show);
 update;
end;

Also tried...

procedure TForm1.Button1Click(Sender: TObject);
begin
    WinExec('C:\Program Files\Apache Tomcat 4.0\bin\shutdown.bat',sw_show);
    update; //refresh form
   // sleep(5000);
    WinExec('C:\Program Files\Apache Tomcat 4.0\bin\startup.bat',sw_show);
    update;
  //  sleep(5000);
end;

 As you can see the objective of my program is to stop and restart the tomcat server. I had tomcat up and running when I tried executing this program and it launches both bat files but it does not shutdown or restart tomcat.

 Since you all say that the delay is not required I commented this part out.

What is the missing piece to make it work?

-ec
0
 

Author Comment

by:entercite
ID: 8089073
One more comment.

 When the startup bat is launched when I used a mouse click on the icon the command window stays open. The delphi exe I made opens and closes both the command windows they dont stay open.

-ec
0
 
LVL 1

Accepted Solution

by:
nex1999 earned 200 total points
ID: 8095183
This worked well for me.

my uses has these:

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Dialogs, StdCtrls, shellapi;


begin
     ShellExecute(0, 'open', 'C:\j2sdk1.4.0_01\bin\java.exe', '-jar -Duser.dir="C:\Program Files\Apache Tomcat 4.0" "C:\Program Files\Apache Tomcat 4.0\bin\bootstrap.jar" stop', nil, SW_SHOW);
     sleep(10000);
     ShellExecute(0, 'open', 'C:\j2sdk1.4.0_01\bin\java.exe', '-jar -Duser.dir="C:\Program Files\Apache Tomcat 4.0" "C:\Program Files\Apache Tomcat 4.0\bin\bootstrap.jar" start', nil, SW_SHOW);
end;
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question