Board index » delphi » executing a dos command

executing a dos command

I have searched every where in books and the help files and I cannot
find out how to execute a dos command from inside a Delphi 4
application.  The closest I can get so far is a WinExec command.  All I
want to do is a directory of a folder and send the result to a text
file.  eg.

var
  strCommand: String;

begin
  strCommand := 'dir c:\ /s/b/a-d >myfile.txt';
  //produces a text list of all files in c:\
  Windows.WinExec(PChar(strCommand), SW_HIDE);//this doesn't work!
......
end;

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

 

Re:executing a dos command


t.h...@trl.telstra.com.au schrieb:

Quote

> begin
>   strCommand := 'dir c:\ /s/b/a-d >myfile.txt';
>   //produces a text list of all files in c:\
>   Windows.WinExec(PChar(strCommand), SW_HIDE);//this doesn't work!
> ......

DIR is an internal command in command.exe, e. g. there is no dir.exe
program. Use

strCommand := 'command.com /c dir c:\ ...(your stuff)'

or even better instead command.com the Result of
GetEnvironmentVariable('comspec'), since NT uses another name as
command.com:

procedure TForm1.Button1Click(Sender: TObject);
var S: Array[0..255] of Char;
begin
  GetEnvironmentVariable('comspec', S, SizeOf(S)-1);
  StrCat(S, ' /c dir c:\ blabla');
  WinExec(S, SW_SHOWMINIMIZED);
end;

Re:executing a dos command


Hi

Quote

>begin
>  strCommand := 'dir c:\ /s/b/a-d >myfile.txt';
>  //produces a text list of all files in c:\
>  Windows.WinExec(PChar(strCommand), SW_HIDE);//this doesn't work!
>......

var comspec : array[0..511] of char;
begin
  FillChar(comspec, sizeof(comspec), 0);
  GetEnvironmentVariable('comspec', comspec, sizeof(comspec));
  // comspec gets the path to the command shell, command.com or cmd.exe
Windows.WinExec(PChar(comspec+strCommand), SW_HIDE);// should work.

Re:executing a dos command


  Never use WInExec in a 32bit application...  Use ShellExecute or
CreateProcess instead.....

--
Jason Wallace
SL Software          Bomb Tech:  If you see me running, try and keep up!"
Home:  Dark...@SLSoftware.reno.nv.us
Work:  Jason_Wall...@Intuit.com
--

Quote
<t.h...@trl.telstra.com.au> wrote in message

news:7rn7b6$mnq$1@nnrp1.deja.com...
Quote
> I have searched every where in books and the help files and I cannot
> find out how to execute a dos command from inside a Delphi 4
> application.  The closest I can get so far is a WinExec command.  All I
> want to do is a directory of a folder and send the result to a text
> file.  eg.

> var
>   strCommand: String;

> begin
>   strCommand := 'dir c:\ /s/b/a-d >myfile.txt';
>   file://produces a text list of all files in c:\
>   Windows.WinExec(PChar(strCommand), SW_HIDE);//this doesn't work!
> ......
> end;

> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.

Re:executing a dos command


Quote
Jason Wallace wrote:
>   Never use WInExec in a 32bit application...  Use ShellExecute or
> CreateProcess instead.....

    How does that help him get the DOS command to work?
Quote

> --
> Jason Wallace
> SL Software          Bomb Tech:  If you see me running, try and keep up!"
> Home:  Dark...@SLSoftware.reno.nv.us
> Work:  Jason_Wall...@Intuit.com
> --

> <t.h...@trl.telstra.com.au> wrote in message
> news:7rn7b6$mnq$1@nnrp1.deja.com...
> > I have searched every where in books and the help files and I cannot
> > find out how to execute a dos command from inside a Delphi 4
> > application.  The closest I can get so far is a WinExec command.  All I
> > want to do is a directory of a folder and send the result to a text
> > file.  eg.

> > var
> >   strCommand: String;

> > begin
> >   strCommand := 'dir c:\ /s/b/a-d >myfile.txt';
> >   file://produces a text list of all files in c:\
> >   Windows.WinExec(PChar(strCommand), SW_HIDE);//this doesn't work!
> > ......
> > end;

> > Sent via Deja.com http://www.deja.com/
> > Share what you know. Learn what you don't.

Re:executing a dos command


Put you dir and redirection in a xx.bat and then use WInExec or
ShellExecute to execute it. This works, I do it in a Delphi console app at
work (windows delphi works also)

Re:executing a dos command


In article <djYD3.2350$TD5.115...@eagle.america.net>,
  "George R Smith" <grsm...@pgtc.net> wrote:

Quote
> Put you dir and redirection in a xx.bat and then use WInExec or
> ShellExecute to execute it. This works, I do it in a Delphi console
app at
> work (windows delphi works also)

I actually thought of that last night and I will be trying it today.  I
have tried some of the suggestions made previously using WinExec.  I
have built up the command line, it compiles, it looks correct in the
de{*word*81}, it executes, produces a valid return value, no exceptions etc.
but the file I am sending the "dir" to does not exist!  Still haven't
worked out why.  I figured a bat file may be an easy way out.

Trevor

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

Re:executing a dos command


th...@my-deja.com schrieb:

Quote

> but the file I am sending the "dir" to does not exist!  Still haven't

Maybe you have to specify the comlete path for the output file, like
'c:\tmp\myfile.txt'

-Michael

Re:executing a dos command


Hi Michael,

When specifying the complete path your solution works great!
You gave me the same solution 2 weeks ago and I have no problems.
Thanks for your help back then.

Luisa

Michael Winter skrev i meddelelsen <37E1DAEC.DA6B...@aquila.de>...

Quote
>th...@my-deja.com schrieb:

>> but the file I am sending the "dir" to does not exist!  Still haven't

>Maybe you have to specify the comlete path for the output file, like
>'c:\tmp\myfile.txt'

>-Michael

Re:executing a dos command


using 'dcc32.exe -CC test.dpr' - this will execute the dos commands

program test;
uses system32, dos;
var a, b : string[255];
begin

{ SwapVectors; }

a := 'c:\sys\command.com';
b := '/C portfns.exe';
exec(a,b);

{ exec('c:\sys\command.com','/C dir'); }
{ exec('c:\sys\command.com','/c prompt enter cmds at your risk : '); }

{ SwapVectors; }

end.

Quote
t.h...@trl.telstra.com.au wrote:

> I have searched every where in books and the help files and I cannot
> find out how to execute a dos command from inside a Delphi 4
> application.  The closest I can get so far is a WinExec command.  All I
> want to do is a directory of a folder and send the result to a text
> file.  eg.

> var
>   strCommand: String;

> begin
>   strCommand := 'dir c:\ /s/b/a-d >myfile.txt';
>   //produces a text list of all files in c:\
>   Windows.WinExec(PChar(strCommand), SW_HIDE);//this doesn't work!
> ......
> end;

> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.

Re:executing a dos command


In article <37E1DAEC.DA6B...@aquila.de>,
  Michael Winter <m...@aquila.de> wrote:

Quote
> th...@my-deja.com schrieb:

> > but the file I am sending the "dir" to does not exist!  Still
haven't

> Maybe you have to specify the comlete path for the output file, like
> 'c:\tmp\myfile.txt'

> -Michael

Upon further investigation I have found a few "funny" things.  If I step
through the code, the file exists.  When I run it at full speed, it does
not exist.  If I sleep for a while after executing the command, the file
will exist.  The only "reasonable" explaination I can find is that the
file IS created, but it takes a short period of time before the file is
actually flushed from RAM onto the disk and into the directory
structure.  If I check for the file immediately after "creating" it,
it is still in RAM, and has not been entered into the directory
structure.  As a result, I Sleep(1000) after executing the command and I
have not problems!  Maybe I should never have sold my old 286! then I
would have plenty of time for it to be produced.

Trevor

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

Re:executing a dos command


th...@my-deja.com schrieb:

Quote
> Upon further investigation I have found a few "funny" things.  If I step
> through the code, the file exists.  When I run it at full speed, it does
> not exist.  If I sleep for a while after executing the command, the file
> will exist.  The only "reasonable" explaination I can find is that the
> file IS created, but it takes a short period of time before the file is
> actually flushed from RAM onto the disk and into the directory
> structure.  If I check for the file immediately after "creating" it,
> it is still in RAM, and has not been entered into the directory
> structure.  As a result, I Sleep(1000) after executing the command and I
> have not problems!  Maybe I should never have sold my old 286! then I
> would have plenty of time for it to be produced.

You didn't guess right. Even on a 386 you would have exactly the same
problem (Win 32 would not run on your 286). What goes on: Calling
WinExec, ShellExecute, CreateProcess or whatever you call to "run" an
external application starts that new process an returns nearly
immidiately without waiting for the end of that process. So if you run
your app with "full speed", the other process doesn't have enough time
to complete, that is to create and write the file, before Windows
schedules back to  your app. But if you "break" your app with the
de{*word*81} or a Sleep() statement, there seems to be enough time to do the
things you expect to be done before you get control back.

A clean solution (maybe sometimes 1000 milliseconds are not enough)
would be using CreateProcess to start the external app, and then use
then ProcessInformation.hProcess handle for a call to
WaitForSingleObject(), which will stop your app until the other one
terminates.

HTH
-Michael

Re:executing a dos command


In article <37E5D8AC.2AFE4...@aquila.de>,
  Michael Winter <m...@aquila.de> wrote:

Quote
> th...@my-deja.com schrieb:
[snip my old text]
> You didn't guess right. Even on a 386 you would have exactly the same
> problem (Win 32 would not run on your 286). What goes on: Calling
> WinExec, ShellExecute, CreateProcess or whatever you call to "run" an
> external application starts that new process an returns nearly
> immidiately without waiting for the end of that process. So if you run
> your app with "full speed", the other process doesn't have enough time
> to complete, that is to create and write the file, before Windows
> schedules back to  your app. But if you "break" your app with the
> de{*word*81} or a Sleep() statement, there seems to be enough time to do
the
> things you expect to be done before you get control back.

> A clean solution (maybe sometimes 1000 milliseconds are not enough)
> would be using CreateProcess to start the external app, and then use
> then ProcessInformation.hProcess handle for a call to
> WaitForSingleObject(), which will stop your app until the other one
> terminates.

> HTH
> -Michael

I will give that a go and see what happens.

Trevor

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

Other Threads