Solved

Executing DOS commands

Posted on 2002-03-21
4
585 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:RQuadling
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now