Board index » delphi » Execute DOS Command from Delphi 1

Execute DOS Command from Delphi 1

I am trying to execute DOS commands from Delphi 1.
I am using WinExec with no problem if the executable is a dos
executable file, but no luck when trying to execute a command
, such as 'del filename' and 'echo xxx > filename'.

Any help would be appreciated.

Thanks in advance.

 

Re:Execute DOS Command from Delphi 1


In article <34f46c1f.22453616@news>, Hugh W. Stewart <hstew...@tiac.net> writes

Quote
>I am trying to execute DOS commands from Delphi 1.
>I am using WinExec with no problem if the executable is a dos
>executable file, but no luck when trying to execute a command
>, such as 'del filename' and 'echo xxx > filename'.

Create a batch file and execute that.

var
  f : TextFile;
begin
  AssignFile(f, 'c:\mybatch.bat');
  ReWrite(f);

  WriteLn(f, 'echo Deleting Temp files...');
  WriteLn(f, 'del *.tmp');

  CloseFile(f); //file must be closed before execution

  //SW_HIDE stops the user seeing the DOS box
  if ShellExecute(Handle, nil, 'c:\mybatch.bat', nil, nil, SW_HIDE) < 32 then
    MessageBox(Handle, 'Error executing DOS command', PChar(Application.Title),
      MB_ICONEXCLAMATION);

end;
--
Jeremy Collins

Re:Execute DOS Command from Delphi 1


Hi,
   Try placing 'COMMAND /C ' before the dos command you wish to execute... this
will cause a temporary copy of command.com to load, the /c will cause it to
execute the command after it and terminate... you can try it at the dos prompt
as well:

     C:\>command /c dir

   will cause a copy of command.com to load, do a dir and exit back (in this
case to the command.com C:\> prompt that was already there.  Good luck!
Mark

Re:Execute DOS Command from Delphi 1


Quote
Hugh W. Stewart wrote:

> I am trying to execute DOS commands from Delphi 1.
> I am using WinExec with no problem if the executable is a dos
> executable file, but no luck when trying to execute a command
> , such as 'del filename' and 'echo xxx > filename'.

> Any help would be appreciated.

> Thanks in advance.

Try WinExec( 'C:\COMMAND.COM /C del <filename>' ).
                             ^
                             (Thought it was /C, check DOS manual...)

(del, copy, echo etc. are built into the DOS command processor,
command.com).

JdV!!

--
========================================================================
Jan de Visser                          etmjdvi@_aom_.ericsson.se
M?/ETX/A/B NMAC Team SAC               Remove underscores
tel. +46 31 74 71890                   ECN 865 71890
                    Vette pech voor de kabouters!
========================================================================

Re:Execute DOS Command from Delphi 1


Many thanks to those who responded.  The combination that worked was
var
Cm: String;
Ln: Array[0..100] of Char;
Hx: THandle;
begin
Cm := 'c:\winnt35\system32\command.com /c echo xx > testfile';
Edit2.Text := Cm;
StrPCopy(Ln, Cm);
Hx := WinExec(@Ln, SW_SHOWNORMAL);
Edit1.Text := IntToStr(Hx);
end;

Note that the .com MUST be included.

On Wed, 25 Feb 1998 19:11:29 GMT, hstew...@tiac.net (Hugh W. Stewart)
wrote:

Quote
>I am trying to execute DOS commands from Delphi 1.
>I am using WinExec with no problem if the executable is a dos
>executable file, but no luck when trying to execute a command
>, such as 'del filename' and 'echo xxx > filename'.

>Any help would be appreciated.

>Thanks in advance.

Other Threads