Help request
I would like to know if there is any way i can execute a .bat file with my free pascal.
(There was more to the message, but this was the main question)
To execute an external program from within the pascal code is a simple process. TProccess will do it quickly. TProcess requires Process added to the uses area. This the setup:
uses Process;
var
RunProgram: TProcess;
begin
RunProgram := TProcess.Create(nil);
RunProgram.CommandLine := ‘Path and Name of Program’;
RunProgram.Execute;
RunProgram.Free;
end;
The Code Used In This Tutorial:
program Project1;
{$mode objfpc}{$H+}
uses
process,
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes
{ you can add units after this };
var
RunProgram: TProcess;
begin
RunProgram := TProcess.Create(nil);
RunProgram.CommandLine := 'calc.exe';
RunProgram.Execute;
RunProgram.CommandLine := 'c:\test.bat';
RunProgram.Execute;
RunProgram.Free;
end.
The batch .bat code used in the video tutorial:
@Echo "Hello This Is The Batch File"
@Echo ""
@pause
notepad.exe
@Time
@pause
Links of Interest:
Lazarus (Free Pascal) Wiki article on executing external programs.
http://wiki.lazarus.freepascal.org/Executing_External_Programs
TProcess Code:
http://lazarus-ccr.sourceforge.net/docs/fcl/process/tprocess.html
TProcess Help For Mac:
http://www.mail-archive.com/lazarus@lazarus.freepascal.org/msg01971.html
TProcess Help For Linux:
http://www.mail-archive.com/lazarus@miraclec.com/msg20819.html
Video: Pascal Helper Series Video 4 - Running External Programs and Batch Scripts (5:40)
Questions for this series can be submitted at YouTube Personal Message System. Our YouTube name is SchoolFreeware
