Link to home
Start Free TrialLog in
Avatar of TRG00
TRG00

asked on

Executing Tree DOS function in Delphi?

Hi, want to display or save the DOS tree function of a particular directory depending on the RadioButton. It only works some times, if I click the button twice..five times, tried putting Application.ProcessMessages into the code but did not work.

Sorry for all the code, but maybe it could help.
Thanks
TRG00


CODE:
procedure TForm1.SpeedButton6Click(Sender: TObject);
var sSaveDir, sTreeDir, sTreeCommand, sTreeDisplay, sDisAllFile :String;
    TextF, TextF2, TextFCreate    : TextFile;
begin
    if CheckBox1.Checked = true then sDisAllFile := '/f'
    else sDisAllFile := '';
    sTreeDir := stvSelectDir.Path;
    AssignFile(TextF2, 'TreeCom.bat');
    {$I-} Rewrite(TextF2); {$I+}
     if IOResult <> 0 then ShowMessage('Error in saving the file!');

    if RadioButton4.Checked = true then
    begin
      SaveDialog1.Execute;
      sSaveDir := SaveDialog1.FileName;
        AssignFile(TextFCreate, sSaveDir);
       {$I-} Rewrite(TextFCreate); {$I+}
        if IOResult <> 0 then  ShowMessage('Error in saving the file!');
        CloseFile(TextFCreate);
        sTreeCommand := 'tree.com ' +ExtractShortPathName(sTreeDir) +' /a'+sDisAllFile +' >> '+ExtractShortPathName(sSaveDir);
        lblDis.caption := sTreeCommand;
        Write(TextF2,sTreeCommand);
        closeFile(TextF2);
        ShellExecute(0, 'open', 'TreeCom.bat', '', '', SW_HIDE);
        Application.ProcessMessages;
   end;

    if RadioButton3.Checked = true then
    begin
      lstTree.clear;
      sTreeCommand := 'tree.com ' +ExtractShortPathName(sTreeDir) +' /a'+sDisAllFile +' >> ' + 'Tree.txt';
      Write(TextF2,sTreeCommand);
      closeFile(TextF2);
      Application.ProcessMessages;
      ShellExecute(0, 'open', 'TreeCom.bat', '', '', SW_HIDE);
      AssignFile(TextF, 'Tree.txt');
      {$I-}  Reset(TextF); {$I+}
       if IOResult <> 0 then ShowMessage('Error creating the file!');
      while not eof(TextF) do
      begin
        Readln(TextF, sTreeDisplay);
        lstTree.Items.Add(sTreeDisplay);
      end;
      closefile(TextF);
      DeleteFile('Tree.txt');
    end;
end;
ASKER CERTIFIED SOLUTION
Avatar of Eddie Shipman
Eddie Shipman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think it is because you are not waiting for your batch file to finish.
try using createprocess which can wait as follows :-

global variables

  MyHandle : THandle;
  si : _STARTUPINFOA;
  pi :_PROCESS_INFORMATION;

      CreateProcess(pchar('Treecom.bat'),pchar(''),nil,nil,false,0,nil,nil,si,pi);
      MyHandle:=pi.hProcess;
      WaitForSingleObject(MyHandle,100000);

100000 is a timeout to wait for in milliseconds, I think there is avalue to set it to infinite if necessary.

Avatar of TRG00
TRG00

ASKER

I tried EddieShipman solution, and it is working thanks.
JDuncan thanks for your solution, is shorter but EddieShipman was just faster.  :-(  

CIA thanks again