Solved

Executing DOS commands

Posted on 2002-03-21
4
613 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
proper way to parse url in delphi 2 197
Delphi - replicating a form 8 73
Correct Component for Shopping Cart. 2 105
Dev express lookupcombo 3 26
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

786 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