Solved

Deleting a directory

Posted on 1998-11-12
4
170 Views
Last Modified: 2011-09-20
Hi

i am currently using SHFileOperation to manipulate my files... it's OK but too inflexible, for example, it will leave things untidy if i try to delete a directory and its got locked files.

I was wondering if someone has written a better piece of code to delete all files and subdirectories from a directory using Delphi's normal file operations like reset, erase, etc..

This will give me more control in my application, ie i know exactly which files are locked, and handle them later...

Regards
Keith
0
Comment
Question by:keithcsl
[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
  • 3
4 Comments
 
LVL 4

Accepted Solution

by:
dwwang earned 70 total points
ID: 1346706
Hope this piece of code helps, it is still crude and may have some errors. It just do recursive search and delete files in a directory.

procedure deleteAll(DirName:string);
var
  DirInfo: TSearchRec;
  r : Integer;
begin
  r := FindFirst(DirName+'\*.*', FaAnyfile, DirInfo);
  while r = 0 do  
    begin
    if DirInfo.Attr and FaDirectory = FaDirectory then
       deleteAll(DirInfo.Name);
    if ((DirInfo.Attr and FaDirectory <> FaDirectory) and
        (DirInfo.Attr and FaVolumeId <> FaVolumeID)) then
       if DeleteFile(pChar(DirName+'\' + DirInfo.Name)) = false then
          ShowMessage('Unable to delete :'+DirName + '\' + DirInfo.Name);
       r := FindNext(DirInfo);
    end;
    SysUtils.FindClose(DirInfo);
    if RemoveDirectory(DirName) = false then
    ShowMessage('Unable to delete direcotry : '+DirName);
end;
0
 
LVL 4

Expert Comment

by:dwwang
ID: 1346707
Sorry, the 10th statement deleteAll(DirInfo.Name);

should be deleteAll(DirName + '\' + DirInfo.Name);
0
 
LVL 4

Expert Comment

by:dwwang
ID: 1346708
Sorry for my boldness, the fully tested version of the procedure should be as:


procedure deleteAll(DirName:string);
var
   DirInfo: TSearchRec;
   r : Integer;
begin
     r := FindFirst(DirName+'\*.*', FaAnyfile, DirInfo);
     while r = 0 do
           begin
           if ((DirInfo.Attr and FaDirectory) = FaDirectory) and (DirInfo.Name<>'.') and (DirInfo.Name<>'..')then
              deleteAll(DirName + '\' + DirInfo.Name);
           if ((DirInfo.Attr and FaDirectory <> FaDirectory) and
               (DirInfo.Attr and FaVolumeId <> FaVolumeID)) then
               if DeleteFile(pChar(DirName+'\' + DirInfo.Name)) = false then
                  ShowMessage('Unable to delete :'+DirName + '\' + DirInfo.Name);
           r := FindNext(DirInfo);
           end;
     SysUtils.FindClose(DirInfo);
     if RemoveDirectory(pchar(DirName)) = false then
        ShowMessage('Unable to delete direcotry : '+DirName);
end;

0
 
LVL 1

Author Comment

by:keithcsl
ID: 1346709
dwwang

thanks for that, it works well

Keith
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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.

688 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