Solved

Executing DOS commands

Posted on 2002-03-21
4
625 Views
Last Modified: 2010-04-05
Hi!

How can I execute a DOS command in Delphi, and get the results from it put into a memo?

ie. I want to execute a DIR command with some switches, and have the output from the DIR put into a memo.  Also, I will need to use the rename command, although if I can get the file name from the DIR, then I can do this programmatically in Delphi!

I'm using Delphi 6 Enterprise...  Thanks for your help...

mdavis1982
0
Comment
Question by:mdavis1982
4 Comments
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 6885530
There are several ways.

1 - You could try using ShellExecute to run the command CMD.EXE or COMAMND.COM with the paramater of "/cDIR directory > LOG.TXT"

2 - You could create a batch file containing all the code you want WITH the redirection to a text file already done and then ShellExecute that batch file.

Once you've got the output file, you can load it into your memo.

3 - You could use something like the code that is available at ...

http://www.delphifreestuff.com/examples/redir.html

This code still stores the output into a file, but you could probably modify it to feed your memo instead.

I don't know if this code is D6 compatible, but it is a start point maybe.

Regards,

Richard Quadling.
0
 
LVL 2

Expert Comment

by:craig_capel
ID: 6887569
When someone has given you all the information you need to complete your task, look at thier comment box and in the top right you will see "accept comment as answer" click on it when you feel they have answered your question. the other time is when the user selects thier comment as answer, however this does not happen often because it's polite to let the user decide if it helped them or not.

-

Craig C.
0
 

Accepted Solution

by:
Rabster earned 50 total points
ID: 6888233
You could use this code:

procedure RunDosInMemo(DosApp:String;AMemo:TMemo);
const
  ReadBuffer = 2400;
var
  Security            : TSecurityAttributes;
  ReadPipe,WritePipe  : THandle;
  start               : TStartUpInfo;
  ProcessInfo         : TProcessInformation;
  Buffer              : Pchar;
  BytesRead           : DWord;
  Apprunning          : DWord;
begin
  With Security do
  begin
   nlength              := SizeOf(TSecurityAttributes);
   binherithandle       := true;
   lpsecuritydescriptor := nil;
  end;
  if Createpipe (ReadPipe, WritePipe,@Security, 0) then
  begin
    Buffer  := AllocMem(ReadBuffer + 1);
    FillChar(Start,Sizeof(Start),#0);
    start.cb          := SizeOf(start);
    start.hStdOutput  := WritePipe;
    start.hStdInput   := ReadPipe;
    start.dwFlags     := STARTF_USESTDHANDLES +
                        STARTF_USESHOWWINDOW;
    start.wShowWindow := SW_Hide;
    if CreateProcess(nil,PChar(DosApp),@Security,
    @Security,true,NORMAL_PRIORITY_CLASS,nil,nil,
    start,ProcessInfo) then
    begin
      repeat
        Apprunning := WaitForSingleObject
        (ProcessInfo.hProcess,100);
        If StopIt = True then
        begin
          ShowMessage('Program Stopped');
          Break;
          ShowMessage('Program Stopped');
        end;
       Application.ProcessMessages;
      until (Apprunning <> WAIT_TIMEOUT);
      repeat
        BytesRead := 0;
        ReadFile(ReadPipe,Buffer[0],
        ReadBuffer,BytesRead,nil);
        Buffer[BytesRead]:= #0;
        OemToAnsi(Buffer,Buffer);
        AMemo.Text := AMemo.text + String(Buffer);
      until (BytesRead < ReadBuffer);
    end;
  end
  else
  ShowMessage('Could not run this applciation');
  FreeMem(Buffer);
  CloseHandle(ProcessInfo.hProcess);
  CloseHandle(ProcessInfo.hThread);
  CloseHandle(ReadPipe);
  CloseHandle(WritePipe);
end;

0
 

Author Comment

by:mdavis1982
ID: 6888666
Thanks! That's great!  Oh, and here's 20 extra points for answering my question! #:o)

Matt
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Printing problem 2 102
JAudiorecorder record freezing the app 29 74
Firemonkey android show image from resource ? 1 46
how to resize animated Gif image in delphi ? 1 35
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question