Solved

Deleting a directory

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

15 Experts available now in Live!

Get 1:1 Help Now