Solved

Deleting Directories

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month11 days, 1 hour left to enroll

628 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