Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Can I issue a DOS command/call a batch file from Delphi ?

Posted on 1999-07-01
9
Medium Priority
?
197 Views
Last Modified: 2010-04-06
I would like to set off a batch file from Delphi and then let it finish (the Delphi program creates the batch file according to the user and then runs it. If I was using C I guess I'd be able to use "system". How would I go about doing this in Delphi ?

Many thanks


Matt
0
Comment
Question by:paninaro
[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
  • 4
  • 3
  • 2
9 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 300 total points
ID: 1385695
hi paninaro,

try this
  winexec('C:\test.bat',SW_SHOW);  //or SW_HIDE for invisible

meikl
0
 

Author Comment

by:paninaro
ID: 1385696
This method does not wait for the batch file to finish but carries on once the batch file has started. Is there anyway of stopping it until the batch file has completed ?
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1385697
Hi paninaro,

try this one:

unit runThread_;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,
  ExtCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen}
  public
    { Public-Deklarationen}
  end;

var Form1 : TForm1 = nil;

implementation

{$R *.DFM}

const unitName = 'runThread_.';

type TRunThread = class(TThread)
  private
    processHandle : cardinal;
    processReady  : boolean;
    waitingThread : cardinal;
    procedure Execute; override;
  end;

procedure TRunThread.Execute;
begin
  WaitForSingleObject(processHandle,INFINITE);   // This call does not return, unless copy is stopped
  processReady:=true;                            // Set "processReady" flag for main thread
  PostThreadMessage(waitingThread,WM_NULL,0,0);  // Wake up main thread
                                                 // If you call Application.HandleMessage (see below) in the
                                                 // main thread, the main thread is sleeping the most time in
                                                 // winAPI "waitMessage". So we send a "dummy" message in order
                                                 // to let the main thread return from Application.HandleMessage
end;

procedure TForm1.Button1Click(Sender: TObject);
var si  : TStartupInfo;
    pi  : TProcessInformation;
    dw1 : dword;
begin
  enabled:=false;
  caption:='start copy...';
  ZeroMemory(@si,sizeOf(si)); si.cb:=sizeOf(si);
  si.dwFlags:=STARTF_USESHOWWINDOW; si.wShowWindow:=SW_HIDE;
  if CreateProcess(nil,'c:\command.com /c copy c:\autoexec.bat c:\test.bat >c:\output.txt',nil,nil,false,0,nil,nil,si,pi) then begin
    caption:='copy started...';
    with TRunThread.Create(true) do         // create the thread object, but do not start it now...
      try
        processHandle:=pi.hProcess;         // tell the thread what process it has to watch
        processReady:=false;                // flag for the loop (see below)
        waitingThread:=GetCurrentThreadID;  // the current threadID for the wakeup message (see above)
        caption:='wait for copy...';
        Resume;                             // now all information is prepared; so let's start the thread
        repeat
          Application.HandleMessage;        // message loop
        until Application.Terminated or processReady;  // continue with normal program when either the
                                                       // started process has stopped or our program is closed
        caption:='copy stopped...';
      finally Free end;
    GetExitCodeProcess(pi.hProcess,dw1);
    CloseHandle(pi.hThread); CloseHandle(pi.hProcess); // Never forget to close handles...
    caption:='ready... (exitCode='+IntToStr(dw1)+')';
  end else caption:='could not start copy...';
  enabled:=true;
end;

end.

Regards, Madshi.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 27

Expert Comment

by:kretzschmar
ID: 1385698
hi madshi,
i thought also about this,
but this is your metier and i'm not a thief. ;-)
(this solution let collect you much points, isn't so?) ;-)


paninaro,
reject my answer,  
let madshi answer and
grade him.

meikl
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1385699
Hi meikl, yes, I already gained many points for this solution...   :-)

But I have no problem if other experts copy my answers. So you have my official permission to post this code...   :-)

Regards, Madshi.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1385700
thanks madshi,

for the official permission ;-)

by the next chance i will do it,
but i will also tell,
who has done this mental work.

meikl
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1385701
That's nice...  :-)
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1385702
Hello, paninaro, are you there?
0
 

Author Comment

by:paninaro
ID: 1385703
Sorry, bit overworked at the mo and haven't had a chance to check it out yet.

Thanks



0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

705 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