Solved

Deleting Directories

Posted on 1997-05-01
3
283 Views
Last Modified: 2010-04-06
Here is the text of my posting on comp.lang.pascal.delphi.misc:

Hey Delphiers:

I'm hoping this is trivial and that I've just overlooked something
obvious, but here's my problem:

I'm looking to clear out and delete directory structures from Delphi
and I've run into the limitation that RmDir only works on empty
directories. I can write a batch file to perform the function in DOS
and do it that way but then I get an annoying black screen flash. I
implemented a TFileListBox component to first delete all the files
in a directory but then the RmDir fails because it says that the
directory is busy. I've tried freeing the TFileListBox before doing
this but it didn't seem to help. It might also be complicated by
the fact that I've compiled this as a DLL and so the caller app
might somehow be tying the file up...

Anyway, is there an easier way all around to do this?
I sure hope so...  Thanx in advance for any advice y'all have...

- Bob

------------------

I received one good response from Rod Geraghty
(GERA-Tech - Ottawa, Canada -gerarod@ibm.net)
which recommended I use the SHFileOpStruct structure
to move, rename or delete entire directories...
Unfortunately this seems to work only for Windows95 and
not to be an option for NT.

Any ideas?

Thanks in advance...
0
Comment
Question by:sidarthur
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
alona041797 earned 50 total points
ID: 1335850
Create a recursive procedure that removes all files and recurses directories:

Procedure removedir(path:string);

procedure deletefile(n:string);
  var f:file;
  begin
    assignfile(f,n);
    erase(f);
  end;

var sr:tsearchrec;
    n:word;
begin
  n:=findfirst(path+'*.*',faAnyFile,sr);
  while n=0 do begin
    if sr.name[1]<>'.' then begin
      if s.attr and fadirectory = fadirectory then
        removedir(path+s.name+'\')
      else deletefile(path+s.name);
    end;
    n:=findnext(s);
  end;
  rmdir(s);
end;
0
 
LVL 2

Expert Comment

by:alona041797
ID: 1335851
This wasn't checked, maybe there are some small syntax errors...
0
 

Author Comment

by:sidarthur
ID: 1335852
As Alona indicated, there were a few small bugs that needed to be
fixed, as well as a not so minor one regarding the proper freeing
of memory used in the recursive procedure (a FindClose(sr) was
required to make it work) but overall I was impressed with the
speed of the answer.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

734 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