Solved

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

Posted on 2000-04-05
6
289 Views
Last Modified: 2006-11-17
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.
0
Comment
Question by:knowlton
6 Comments
 
LVL 17

Expert Comment

by:inthe
ID: 2687253
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;
0
 
LVL 1

Expert Comment

by:Oli2
ID: 2687389
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
0
 
LVL 9

Accepted Solution

by:
ITugay earned 50 total points
ID: 2689933
You may do it more easy and DOS window would not be displayed.

uses ShellApi;

procedure TFormMain.DeleteTempFolder;
.......
   ShellExecute(handle,'open','command','/c deltree /Y ' +  JARTempDir,nil,0);
end;

-------------
Igor.
0
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.

 
LVL 5

Author Comment

by:knowlton
ID: 2730819
ITugay:

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

Thanks!
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2730922
;)
0
 
LVL 5

Author Comment

by:knowlton
ID: 2730959
=)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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 video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 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

16 Experts available now in Live!

Get 1:1 Help Now