Solved

Deleting Directories

Posted on 1997-05-01
3
258 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
  • 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 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

24 Experts available now in Live!

Get 1:1 Help Now