Solved

Deleting Directories

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Mdi application Child forms get behind control 7 211
Correct Component for Shopping Cart. 2 105
LAN or WAN ? 11 90
Delphi: Connect to running MS Outlook 4 83
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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