Solved

Deleting a directory

Posted on 1998-11-12
4
167 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
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

815 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

10 Experts available now in Live!

Get 1:1 Help Now