9. How do I execute an
MS-DOS command from within a TP program?
Q: How do I execute an MS-DOS command from within a TP
program?
A: The best way to
answer this question is to give an example.
{$M 2048, 0, 0} (* <-- Important *)
program outside;
uses dos;
begin
write ('Directory call from within TP by Timo Salmi');
SwapVectors;
Exec (GetEnv('comspec'), '/c dir *.*'); (* Execution *)
SwapVectors;
(* Testing for errors is recommended *)
if DosError <> 0 then
writeln ('Dos error number ', DosError)
else
writeln ('Mission accomplished, exit code ', DosExitCode);
(* For DosError and DosExitCode details see the TP manual *)
end.
Alternatively, take a look at execdemo.pas from demos.arc which
should be on the disk accompanying Turbo Pascal.
What the above Exec does is that it executes the command
processor. The /c specifies that the command interpreter is to
perform the command, and then stop (not halt).
I have also seen it asked how one can swap the Turbo Pascal
program to the disk when shelling. It is unnecessary to program
that
separately because there is an excellent program to do that for
you.
It is ftp://garbo.uwasa.fi/pc/sysutil/shrom24b.zip.
Somewhat surprisingly some users have had difficulties with
redirecting shelled output. It is straight-forward. In the above
code one would use, for example
Exec (GetEnv('comspec'), '/c dir *.* > tmp');