Solved

Deleting a directory

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

22 Experts available now in Live!

Get 1:1 Help Now