Solved

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

Posted on 2000-04-05
6
294 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:Tom 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:Tom 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:Tom Knowlton
ID: 2730959
=)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Form ownership 4 124
Error E2158 compiling with Delphi XE10 Seattle 2 124
Base1 Encode/Decode 3 85
Microsoft Access 97 and Delphi XE2 9 71
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 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