Solved

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

Posted on 2000-04-05
6
290 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 160
Delphi TcxGrid group footer summary 3 203
drawing animated level bar based on numbers 3 86
FMX and jaudiotracker playing memory stream 29 66
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

930 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

13 Experts available now in Live!

Get 1:1 Help Now