Board index » delphi » Executing batch files.

Executing batch files.

I've written the following program.

-----------------------------------
program theday;
{$M $4000,0,0}
uses dos;
var year,month,date,dw:word;
begin
getdate(year,month,date,dw);
writeln('Todays date is ',month,'-',date,'-',year);
if(month = 5) and (date = 25) then Exec('c:\pascal\job.bat','job');
writeln('Program terminated!');
end.
-----------------------------------

But, when I run it, it runs ok as long as month and date are not equal to
todays month and date. When that happens, it is supposed to execute the
batch file 'job.bat'. But instead of execting job.bat, my system re-boots.
I know this because I've tested it after setting the values of month and
date in the if statement which runs 'Exec' to todays month and date.

Here is what job.bat looks like.

-----------------------------------
mem/c
dir/s c:\*.*
-----------------------------------

What did I do wrong?
--
..........Patrick D. Rockwell..................................................
Internet: prock...@trumpet.calpoly.edu
Prodigy: hnhc85a
America On-Line: patri48975

 

Re:Executing batch files.


Quote
> I've written the following program.
> -----------------------------------
> program theday;
> {$M $4000,0,0}
> uses dos;
> var year,month,date,dw:word;
> begin
> getdate(year,month,date,dw);
> writeln('Todays date is ',month,'-',date,'-',year);
> if(month = 5) and (date = 25) then Exec('c:\pascal\job.bat','job');
> writeln('Program terminated!');
> end.
> -----------------------------------
> But, when I run it, it runs ok as long as month and date are not equal to
> todays month and date. When that happens, it is supposed to execute the
> batch file 'job.bat'. But instead of execting job.bat, my system re-boots.
> I know this because I've tested it after setting the values of month and
> date in the if statement which runs 'Exec' to todays month and date.

> Here is what job.bat looks like.

> -----------------------------------
> mem/c
> dir/s c:\*.*
> -----------------------------------
> What did I do wrong?

   Try:

  SwapVectors;
  Exec (GetEnv('COMSPEC'),'/C job.bat');
  SwapVectors;

Re:Executing batch files.


In article <4o2qaa$4...@trumpet.aix.calpoly.edu>,
Patrick D Rockwell <prock...@trumpet.aix.calpoly.edu> wrote:

Quote
>I've written the following program.

>-----------------------------------
>program theday;
>{$M $4000,0,0}
>uses dos;
>var year,month,date,dw:word;

[snip]

I'm replying to my own post (sorry, :-) ). I forgot to mention that I'm
using TP4.0 so, I don't think that I have the getenv() function.

--
..........Patrick D. Rockwell..................................................
Internet: prock...@trumpet.calpoly.edu
Prodigy: hnhc85a
America On-Line: patri48975

Re:Executing batch files.


Quote
In article <4o2qaa$4...@trumpet.aix.calpoly.edu> Patrick D Rockwell wrote:
>I've written the following program.

>-----------------------------------
>program theday;
>{$M $4000,0,0}
>uses dos;
>var year,month,date,dw:word;
>begin
>getdate(year,month,date,dw);
>writeln('Todays date is ',month,'-',date,'-',year);
>if(month = 5) and (date = 25) then Exec('c:\pascal\job.bat','job');
>writeln('Program terminated!');
>end.
>-----------------------------------

>But, when I run it, it runs ok as long as month and date are not equal to
>todays month and date. When that happens, it is supposed to execute the
>batch file 'job.bat'. But instead of execting job.bat, my system re-boots.
>I know this because I've tested it after setting the values of month and
>date in the if statement which runs 'Exec' to todays month and date.

>Here is what job.bat looks like.

>-----------------------------------
>mem/c
>dir/s c:\*.*
>-----------------------------------

>What did I do wrong?

Executing a batch file that way is having DOS execute the text in the batch
file as if it were machine language.  You need to invoke the DOS shell and
have it process the batch file. I'm not sure why the "job" parameter, but
your example would become -

    Exec(GetEnv('COMSPEC'), '/c c:\pascal\job.bat job'

The leading /c is a parameter to the shell stating that it is to exit after
completing the command.

If I remember correctly, TP 4.0 doesn't have a GetEnv function.  If you
don't have one you might what to pick up the UTIL unit from my home page
rather than hard-wire it to c:\command.com, a:\command.com, etc.

--
RDon...@gnn.com
http://members.gnn.com/rdonais/index.html
-------------------------------------------
Knowledge is one of the few things that you
can give away and still keep for yourself.

Other Threads