Link to home
Start Free TrialLog in
Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

asked on

CreateProcess okay, but DOS "window" doesn't close

Consider the following code:

procedure TFormMain.DeleteTempFolder;
var
  SI : TStartUpInfo;
  PI : TProcessInformation;
  JARTempDir : string;
begin
  FillChar(SI, sizeof(SI), 0);
  SI.cb := sizeof(SI);
  JARTempDir := ExtractFilePath(Application.ExeName) + 'Temp';
  CreateProcess(nil, pchar('C:\windows\command\deltree /Y ' +  JARTempDir),
    nil, nil, False, 0, nil, nil, SI, PI);
end;

CreateProcess opens a DOS window and performs the deltree operation just fine.  BUT, the window stays open.

How do I get the window to close when the deltree operation is complete?

Ideally, the DOS window would not be displayed at all.

Can someone who is well-versed on the CreateProcess function help me out here?

Is there an alternative to CreateProcess I can use to perform the deltree command?  Perhaps some obscure Delphi file i/o command that can delete entire directories?

I know I've asked several questions here, but they all revolve around a common theme:  

Performing "silent" command-line routines.
Avatar of inthe
inthe

Hi
you could try one of these procedures instead :

procedure DeleteFileOrFolder(const FileOrFolder: String; Allowundo: Boolean);
var
  OS: TSHFileOpStruct;
begin
  if Length(FileOrFolder)>0 then
  begin
    FillChar(OS, sizeof(OS),0);
    OS.pFrom := PChar(FileOrFolder);
    OS.wFunc := FO_DELETE;
    OS.fFlags := FOF_NOCONFIRMATION or FOF_SILENT;
    if Allowundo then
      OS.fFlags := OS.fFlags or FOF_ALLOWUNDO;
    if SHFileOperation(OS)<>0 then
      RaiseLastWin32Error
  end
end;
 


procedure DeleteFolderTree(Folder:String);
var
  ErrorCode : Integer;
  SearchRec : TSearchRec;
begin
  try
    ErrorCode := FindFirst(Folder + '\*.*', faAnyFile, SearchRec);     while ErrorCode = 0 do
      begin
        if ((SearchRec.Name <> '.')
        and (SearchRec.Name <> '..'))
        and (SearchRec.Attr <> faVolumeID) then
begin
          if  (SearchRec.Attr and faDirectory>0) then
            DeleteFolderTree (Folder+'\'+SearchRec.Name,SecureDelete)           else
            DeleteFile(Folder+'\'+SearchRec.Name)
          end;
        end;
        ErrorCode := FindNext (SearchRec);
    end;
  finally
    FindClose(SearchRec);
    rmdir (Folder);
  end;
end;
you could do the following:

PostMessage(FindWindow('tty', 'DOS-BOX-CAPTION'), WM_QUIT, 0, 0);

after deltree is done.
replace 'DOS-BOX-CAPTION' with the Title of the DOS-Box.

Regards, Oli
ASKER CERTIFIED SOLUTION
Avatar of Igor UL7AAjr
Igor UL7AAjr
Flag of Kazakhstan 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
Avatar of Tom Knowlton

ASKER

ITugay:

Your answer was short, sweet, and just what I wanted.

Thanks!
=)