Solved

Deleting Directories

Posted on 1997-05-01
3
263 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

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

16 Experts available now in Live!

Get 1:1 Help Now