Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Deleting a directory

Posted on 1998-11-12
4
Medium Priority
?
172 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 280 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

604 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