Board index » delphi » How can I do something like DELTREE in delphi ?

How can I do something like DELTREE in delphi ?

How can I delete an entire directory ? One which may have files and
sub-dirs in it ? Like DELTREE under DOS.

Ive looked at the RmDir procedure but that can only handle empty
directories.

I guess I could just use DirectoryExists (whatever it is) to find out
if it has succesfully been deleted so I guess it wont have to be a
function which returns a value if succesfully deleted etc.

Using Delphi 1 and Delphi 2.01.

Barry

 

Re:How can I do something like DELTREE in delphi ?


In article <35e06e47.1414...@news.demon.co.uk>, ar...@REMOVE-THISbigfoot.com

Quote
(R. Barry Jones) writes:
>How can I delete an entire directory ? One which may have files and
>sub-dirs in it ? Like DELTREE under DOS.

Why not ue Deltree itself? Simply give Deltree as parameter to ShellExecute.

Charles Johnson

Re:How can I do something like DELTREE in delphi ?


On 23 Aug 1998 21:25:52 GMT, cehjohn...@aol.com (CEHJohnson) wrote:

Quote
>In article <35e06e47.1414...@news.demon.co.uk>, ar...@REMOVE-THISbigfoot.com
>(R. Barry Jones) writes:

>>How can I delete an entire directory ? One which may have files and
>>sub-dirs in it ? Like DELTREE under DOS.

>Why not ue Deltree itself? Simply give Deltree as parameter to ShellExecute.

Im not that brilliant using the WINAPI stuff but I had a go but it
didnt work.  I tried doing this in a button's OnClick event :

     shellexecute(handle,
                  'open',
                  pchar('c:\windows\command\deltree.exe'),
                  pchar(edit1.text),
                  Nil,
                  SW_HIDE);

and it didnt delete a directory which I specified in a TEdit (edit1)
qhich was "c:\test" which i had created for this test.  The directory
was still there after I pressed the button.

What did I do wrong ?

Not the best solution though, as the user may not have deltree.exe.

Barry

Re:How can I do something like DELTREE in delphi ?


Deltree.exe requires you to confirm the action by pressing "y/n"...

If you hide the dos box, you can't do it.

Try executing a ShFileOperation instead :

Uses ...,...., ShellAPI;

function DeleteTree(SrcPath : string): boolean;
var
   FileOpStruct : TShFileOpStruct;
begin
   with FileOpStruct do begin
       Hwnd   := Application.Handle;
       wFunc  := foDelete;
       pFrom  := PChar(SrcPath);
       pTo      := NIL;
       Flags   := FOF_NOCONFIRMATION    or FOF_SILENT;
       lpszProgressTitle := NIL;
   end;
   result := ShFileOperation(FileOpStruct) = 0;
end;

This function should do it. I never tried it myself, but i was curious
about how i would it if i had to, so i dug up in the Win32.hlp file.

Maybe it needs a few parameter adjustments, but i think it should work as
is.

Note that you change change the Flags parameter, to ask for user
confirmation, or display a progress box as Explorer does, but from the way
you called deltree.exe, you seemed to want it silent, so i set the
corresponding flags...

Hope it helps!

R. Barry Jones <ar...@REMOVE-THISbigfoot.com> wrote in article
<35e0b7a0.4792...@news.demon.co.uk>...

Quote
> On 23 Aug 1998 21:25:52 GMT, cehjohn...@aol.com (CEHJohnson) wrote:

> >In article <35e06e47.1414...@news.demon.co.uk>,

ar...@REMOVE-THISbigfoot.com
Quote
> >(R. Barry Jones) writes:

> >>How can I delete an entire directory ? One which may have files and
> >>sub-dirs in it ? Like DELTREE under DOS.

> >Why not ue Deltree itself? Simply give Deltree as parameter to
ShellExecute.

> Im not that brilliant using the WINAPI stuff but I had a go but it
> didnt work.  I tried doing this in a button's OnClick event :

>      shellexecute(handle,
>                   'open',
>                   pchar('c:\windows\command\deltree.exe'),
>                   pchar(edit1.text),
>                   Nil,
>                   SW_HIDE);

> and it didnt delete a directory which I specified in a TEdit (edit1)
> qhich was "c:\test" which i had created for this test.  The directory
> was still there after I pressed the button.

> What did I do wrong ?

> Not the best solution though, as the user may not have deltree.exe.

> Barry

Re:How can I do something like DELTREE in delphi ?


In article <35e0b7a0.4792...@news.demon.co.uk>, ar...@REMOVE-THISbigfoot.com

Quote
(R. Barry Jones) writes:
>What did I do wrong ?

>Not the best solution though, as the user may not have deltree.exe.

The good news is there's a simple answer to your problem - DelTree takes a
confirmation parameter after the first param. The bad news is that _I _can't
pass it! -
not even on the command line itself.
It may be that this is such a dangerous command (I've renamed mine) that manual
confirmation is required.

Anyway your point about portability is valid if you want to distribute. A good
one is SHFileOperation, but again portability raises its ugly head.
The blunt instrument of FindFirst...FindNext and DeleteFile will have to be
employed.

Charles Johnson

Re:How can I do something like DELTREE in delphi ?


On 24 Aug 1998 18:22:48 GMT, cehjohn...@aol.com (CEHJohnson) wrote:

Quote
>In article <35e0b7a0.4792...@news.demon.co.uk>, ar...@REMOVE-THISbigfoot.com
>(R. Barry Jones) writes:

>>What did I do wrong ?

>>Not the best solution though, as the user may not have deltree.exe.

>The good news is there's a simple answer to your problem - DelTree takes a
>confirmation parameter after the first param. The bad news is that _I _can't
>pass it! -
>not even on the command line itself.
>It may be that this is such a dangerous command (I've renamed mine) that manual
>confirmation is required.

I managed to do it :)  Your right, you need to confirm it in DOS by
pressing 'y' .. and you can sort of do the same via windows.. well, in
a way.  I did "deltree /?" in a dos box and it gave me a bit of info.
basically, you include a /Y parameter to delete it without confirming.
So, I changed my code to the following and it worked :

     shellexecute(handle,
                  'open',
                  pchar('c:\windows\command\deltree.exe'),
                  pchar('/Y '+edit1.text),   {Notice the /Y}
                  Nil,
                  SW_HIDE);

Quote

>Anyway your point about portability is valid if you want to distribute. A good
>one is SHFileOperation, but again portability raises its ugly head.
>The blunt instrument of FindFirst...FindNext and DeleteFile will have to be
>employed.

Well, Im mainly doing programs under win95 right now so I'll get I'll
try and use the SHFileOperation stuff.  For win3.11, I may try and do
a function sometime which will do the FindFirst...FindNext, DeleteFile
etc ... but Im not patient enough to do that right now :)

Barry

Re:How can I do something like DELTREE in delphi ?


In article <01bdcf3b$5cfc4a60$69a36...@charles.videotron.ca>, Charles
Bedard <beda...@nospam.videotron.ca> writes

Quote
>Deltree.exe requires you to confirm the action by pressing "y/n"...

DelTree also accepts a "/y" parameter to delete a directory
immediately:

C:\>deltree /y windows

does not ask for confirmation.

So I guess you could also do this via ShellExecute.
--
Jeremy Collins
Kansai Business Systems

Re:How can I do something like DELTREE in delphi ?


Quote
R. Barry Jones wrote:

> On 23 Aug 1998 21:25:52 GMT, cehjohn...@aol.com (CEHJohnson) wrote:

> >In article <35e06e47.1414...@news.demon.co.uk>,
> ar...@REMOVE-THISbigfoot.com
> >(R. Barry Jones) writes:

> >>How can I delete an entire directory ? One which may have files and
> >>sub-dirs in it ? Like DELTREE under DOS.

> >Why not ue Deltree itself? Simply give Deltree as parameter to
> ShellExecute.

> Im not that brilliant using the WINAPI stuff but I had a go but it
> didnt work.  I tried doing this in a button's OnClick event :

>      shellexecute(handle,
>                   'open',
>                   pchar('c:\windows\command\deltree.exe'),
>                   pchar(edit1.text),
>                   Nil,
>                   SW_HIDE);

> and it didnt delete a directory which I specified in a TEdit (edit1)
> qhich was "c:\test" which i had created for this test.  The directory
> was still there after I pressed the button.

> What did I do wrong ?

> Not the best solution though, as the user may not have deltree.exe.

> Barry

I did it with a recursive procedure deleting all files and
subdirectories. If you're interested, let me know.

Regards,

Remco de Korte
http://www.xs4all.nl/~remcodek/softmach.html (games - TP & Delphi)

Re:How can I do something like DELTREE in delphi ?


Hi, All!

Quote
R. Barry Jones wrote:
> On 23 Aug 1998 21:25:52 GMT, cehjohn...@aol.com (CEHJohnson) wrote:

> >In article <35e06e47.1414...@news.demon.co.uk>, ar...@REMOVE-THISbigfoot.com
> >(R. Barry Jones) writes:

> >>How can I delete an entire directory ? One which may have files and
> >>sub-dirs in it ? Like DELTREE under DOS.

> >Why not ue Deltree itself? Simply give Deltree as parameter to ShellExecute.

> Im not that brilliant using the WINAPI stuff but I had a go but it
> didnt work.  I tried doing this in a button's OnClick event :

>      shellexecute(handle,
>                   'open',
>                   pchar('c:\windows\command\deltree.exe'),
>                   pchar(edit1.text),
>                   Nil,
>                   SW_HIDE);

> and it didnt delete a directory which I specified in a TEdit (edit1)
> qhich was "c:\test" which i had created for this test.  The directory
> was still there after I pressed the button.

> What did I do wrong ?

> Not the best solution though, as the user may not have deltree.exe.

> Barry

I've thought it over and produced such a function that deletes given directory
with all subdirectories and all files. So try this( it works nice ;) :

function RecursRemoveDir( ADir : string ) : boolean;
var
  Found : integer;
  ASearchRec : TSearchRec;
begin
  Found :=FindFirst( ADir + '\*.*' , faAnyFile , ASearchRec );
  Result :=Found=0;
  while Found = 0 do
  begin
    if ASearchRec.Attr and faDirectory <> 0 then
    begin
      if (ASearchRec.Name <>'.') and (ASearchRec.Name <>'..') then
        Result :=Result and RecursRemoveDir( ADir + '\' + ASearchRec.Name );
    end
    else
      try
        Application.ProcessMessages;
        Result :=Result and DeleteFile( ADir + '\' + ASearchRec.Name );
      except
        Result :=False;
        raise;
      end;
    Found :=FindNext( ASearchRec );
  end;
  try
    Result :=Result and RemoveDir( ADir );
  except
    Result :=False;
    raise;
  end;
  FindClose( ASearchRec );
end;

---------
Sincerely,
Vadim Gurevich
M&M Data Systems, Ukraine
e-mail: va...@mmdata.kharkov.ua

Re:How can I do something like DELTREE in delphi ?


Vadim's solution looks good, although I haven't tested it. What's more
important is that it would be the most portable.

Now why the ?*&%! does the confirmation parameter to DelTree come _before_ the
directory name on the command line!?

Charles Johnson

Re:How can I do something like DELTREE in delphi ?


Traverse the directories, deleting as you go. You can use the DirScan
component to help you with the traversal. It is a free VCL component,
available at

        http://sunsite.icm.edu.pl/delphi

Look for dirscan.zip.

        Michael Burton
        AMS Services, Inc.
        Coeur d'Alene, ID

On Sun, 23 Aug 1998 19:32:49 GMT, ar...@REMOVE-THISbigfoot.com (R.

Quote
Barry Jones) wrote:
>How can I delete an entire directory ? One which may have files and
>sub-dirs in it ? Like DELTREE under DOS.

Re:How can I do something like DELTREE in delphi ?


On 24 Aug 1998 18:22:48 GMT, cehjohn...@aol.com (CEHJohnson) wrote:

Quote
>The good news is there's a simple answer to your problem - DelTree takes a
>confirmation parameter after the first param. The bad news is that _I _can't
>pass it! -
>not even on the command line itself.

<snip>

Quote
>Charles Johnson

ShellExecute(Handle, 'open', 'Deltree', '/Y C:\Dir\To\Delete', nil, SW_HIDE);
This works fine on my system. But I agree that Deltree should be avoided
for portability reasons.

Dave

======================================================
Test yourself by mankind. It makes the doubter doubt,
the believer believe.

Franz Kafka

Re:How can I do something like DELTREE in delphi ?


ar...@REMOVE-THISbigfoot.com (R. Barry Jones) wrote:

Quote
>How can I delete an entire directory ? One which may have files and
>sub-dirs in it ? Like DELTREE under DOS.
>Ive looked at the RmDir procedure but that can only handle empty
>directories.

On my web pages at www.kobayashi.com, follow the Freeware link. There
you'll find an old dos program I wrote called KD (kill directory). It
does exactly what you're looking for. The (pascal) source code is
included in the archive, and you're welcome to use it.

--
Kobayashi Software - Chicago, Illinois
www.kobayashi.com - e...@kobayashi.com

Re:How can I do something like DELTREE in delphi ?


In article <35e41f80.12301...@news.{*word*269}.net>, david...@{*word*269}.net (David Ku)
writes:

Quote
>ShellExecute(Handle, 'open', 'Deltree', '/Y C:\Dir\To\Delete', nil, SW_HIDE);
>This works fine on my system. But I agree that Deltree should be avoided
>for portability reasons.

As a straight command    Echo Y | Del *.*   responds with the Y to the
confirmation request, couldn't you do the same with DelTree.

Alan Lloyd
alangll...@aol.com

Other Threads