?
Solved

Executing DOS commands

Posted on 2002-03-21
4
Medium Priority
?
660 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 200 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

765 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