Execute DOS-command/program from a program. 
Author Message
 Execute DOS-command/program from a program.

Hi!

I am trying to make a program for registration of computers in our
network.

When searching in Help in BP7.0 i found this sample code:
  { Example for DosExitCode and Exec }

  {$M $4000,0,0 }   { 16K stack, no heap }
  uses Dos;
  var
    ProgramName, CmdLine: string;
  begin
    Write('Program to Exec (full path): ');
    ReadLn(ProgramName);
    Write('Command line to pass to ',
          ProgramName, ': ');
    ReadLn(CmdLine);
    WriteLn('About to Exec...');
    SwapVectors;
    Exec(ProgramName, CmdLine);
    SwapVectors;
    WriteLn('...back from Exec');
    if DosError <> 0 then{ Error? }
      WriteLn('Dos error #', DosError)
    else
      WriteLn('Exec successful. ',
              'Child process exit code = ',
              DosExitCode);
  end.

It works OK, but I would like to avoid knowing the path, and just use
the paths already loaded (either in autoexec.bat or in an other
bat-file), and I would also like to pipe the information I get from the
program into a file or a string, so that I can use the information
later.
I have tried to type "exefile > test.txt" without any success.

If anyone have some idears of how to solve this I would be happy.

Thanks

Bj?rge Stavik



Wed, 18 Jun 1902 08:00:00 GMT  
 Execute DOS-command/program from a program.
Hi!!

...

Quote:
> It works OK, but I would like to avoid knowing the path, and just use
> the paths already loaded (either in autoexec.bat or in an other
> bat-file)

For this case, you don't have to EXEC the EXE-File directly, but by
using COMMAND.COM.
Example:
 Instead of
  EXEC( exename, '' );
 use
  EXEC( '\COMMAND.COM','/C '+exename );
You should read the enviroment COMSPEC= to see where COMMAND.COM is
located. If there is no COMSPEC=, it should be in the root directory.
For help on COMMAND.COM type COMMAND /?.

Quote:
>, and I would also like to pipe the information I get from the
> program into a file or a string, so that I can use the information
> later.
> I have tried to type "exefile > test.txt" without any success.

You must do
 EXEC( exefile, '> test.txt' );

Hope, this helps you!

Arno Fehm



Wed, 18 Jun 1902 08:00:00 GMT  
 Execute DOS-command/program from a program.

Quote:
>Hi!!


>...
>> It works OK, but I would like to avoid knowing the path, and just use
>> the paths already loaded (either in autoexec.bat or in an other
>> bat-file)
>For this case, you don't have to EXEC the EXE-File directly, but by
>using COMMAND.COM.
>Example:
> Instead of
>  EXEC( exename, '' );
> use
>  EXEC( '\COMMAND.COM','/C '+exename );

No no no. The proper way is to do exec(getenv('comspec'))...

There is no justification for hard coding path names.

Osmo



Wed, 18 Jun 1902 08:00:00 GMT  
 Execute DOS-command/program from a program.
On Wed, 17 Feb 1999 10:17:37 -0800, Bj?rge Stavik

Quote:

> It works OK, but I would like to avoid knowing the path, and just use
> the paths already loaded (either in autoexec.bat or in an other
> bat-file), and I would also like to pipe the information I get from the
> program into a file or a string, so that I can use the information
> later.
> I have tried to type "exefile > test.txt" without any success.

> If anyone have some idears of how to solve this I would be happy.

If you want PATH searching, redirection (<,<<,>>,>) and piping (|) you
have either to do it "by hand" or you have to call your child program
through a command processor because these are features of a specific
command shell which are not supported by a straightforward call to the
bare DOS Exec function.

        exec(getenv('COMSPEC'),'/cmyprog > bla');

I.e. you have to 'exec' the command processor referred to by the
environment variable COMSPEC (or any other command processor of your
choice which you know to be present at run time) and to give it as
parameter the appropriate command line in order to execute it as a
secondary shell.

Note that DosExitCode will return in this case the exit code of the
command processor - the program executed by 'exec' - and not the exit
code of the command executed by the command processor.

Regards
Horst



Wed, 18 Jun 1902 08:00:00 GMT  
 Execute DOS-command/program from a program.

Quote:

>   EXEC( '\COMMAND.COM','/C '+exename );

Anyone working for me who wrote code like this would be placed upside
down in a bucket of wet cement. :-)

*Never* code hard-wired path names! To find COMMAND.COM use
GetEnv('COMSPEC') instead.

Mike{*filter*}son, Black Cat Software Factory, Edinburgh, Scotland
fax 0131-271-1551 - Columnated Ruins Domino - Mellotron M400 #996



Wed, 18 Jun 1902 08:00:00 GMT  
 Execute DOS-command/program from a program.

Quote:

> > Instead of
> >  EXEC( exename, '' );
> > use
> >  EXEC( '\COMMAND.COM','/C '+exename );

> No no no. The proper way is to do exec(getenv('comspec'))...

> There is no justification for hard coding path names.

Ok, Ok!!
Maybe, I did a mistake, but i acutally ment this by writing:
Quote:
> You should read the enviroment COMSPEC= to see where COMMAND.COM is
> located. If there is no COMSPEC=, it should be in the root directory.
> For help on COMMAND.COM type COMMAND /?.

The next time, I will try to express myself a bit better...

But are you completely sure, that COMSPEC is _always_ defined? Even if
COMMAND.COM is located in the root directory?
For that case, exec( getenv('comsped') ) would bring wrong results.

Bye!
Arno Fehm



Wed, 18 Jun 1902 08:00:00 GMT  
 Execute DOS-command/program from a program.


Quote:
> But are you completely sure, that COMSPEC is _always_ defined? Even if
> COMMAND.COM is located in the root directory?

Yes. COMSPEC is set automatically to BootDrive:\COMMAND.COM at startup
and by

SHELL = .... /P

in CONFIG.SYS

Quote:
> For that case, exec( getenv('comsped') ) would bring wrong results.

On a correctly maintained DOS system COMSPEC is _always_ defined. If
the user deletes it by typing:

c:\> SET COMSPEC=

then exec(getenv('COMSPEC'),...) would bring _no_ results, of course
..

Regards
Horst



Wed, 18 Jun 1902 08:00:00 GMT  
 Execute DOS-command/program from a program.

Quote:

>But are you completely sure, that COMSPEC is _always_ defined? Even if
>COMMAND.COM is located in the root directory?
>For that case, exec( getenv('comsped') ) would bring wrong results.

It is the responsibility of the user to make sure that it is loaded.

Osmo



Wed, 18 Jun 1902 08:00:00 GMT  
 Execute DOS-command/program from a program.

Quote:

>On a correctly maintained DOS system COMSPEC is _always_ defined.

Do you happen to know off-hand which version of DOS that started in? I
don't have anything old enough not to have it, but I'm sure it wasn't
in the really old versions. Did it arrive around DOS 5 or so?

FP



Wed, 18 Jun 1902 08:00:00 GMT  
 Execute DOS-command/program from a program.

Quote:


> >On a correctly maintained DOS system COMSPEC is _always_ defined.

> Do you happen to know off-hand which version of DOS that started in? I
> don't have anything old enough not to have it, but I'm sure it wasn't
> in the really old versions. Did it arrive around DOS 5 or so?

It was beginning to become useful with DOS 3.0, because with DOS 2
it was not yet possible to invoke a .COM or .EXE with a path spec.

It was not useful to call C:\MSDOS\COMMAND.COM if C:\MSDOS was
neither in the PATH nor the actual directory.

So I remember that COMSPEC was available with DOS 2, but more or
less useless.

Franz Glaser
--
Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria ++43-7219-7035
Muehlviertler Elektronik Glaser. Industrial control and instrumentation
-----------------------------------------------------------------------

http://www.geocities.com/~franzglaser  http://members.xoom.com/f_glaser



Wed, 18 Jun 1902 08:00:00 GMT  
 Execute DOS-command/program from a program.


Quote:


> >On a correctly maintained DOS system COMSPEC is _always_ defined.

> Do you happen to know off-hand which version of DOS that started in? I
> don't have anything old enough not to have it, but I'm sure it wasn't
> in the really old versions. Did it arrive around DOS 5 or so?

I'm using DOS since v3.0 and I'm sure that from 3.0 on it was always
created and maintained by DOS and was usable by programs.

Regards
Horst



Wed, 18 Jun 1902 08:00:00 GMT  
 Execute DOS-command/program from a program.

Quote:

>I'm using DOS since v3.0 and I'm sure that from 3.0 on it was always
>created and maintained by DOS and was usable by programs.

Thanks. (I've been using DOS since v1.25 but I don't have the old
stuff to hand, and I'm not sure what I was eating for breakfast last
week, never mind what environment variables I had 10 years ago.)

FP



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Execute DOS-command/program from a program.

2. How too execute a Dos program

3. Executing DOS batch files (or any other program files) and deleting files with PASCAL

4. Help: Using WinExec to execute a DOS program.

5. Can Delphi compiled programs execute under DOS?

6. Can Delphi compiled programs execute under DOS?

7. executing dos programs in tp

8. executing dos programs in tp

9. Calling Dos commands from inside a program?

10. running dos commands from with a pascal program?

11. Run ms-dos command in Pascal program

12. Run ms-dos command in Pascal program

 

 
Powered by phpBB® Forum Software