Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

failing to delete file?

Status
Not open for further replies.

CADTenchy

Technical User
Dec 12, 2007
237
0
0
GB
Hi,

I'm trying to delete a directory if it exists.
it works if the directory is empty, but if I put a file in there it fails.
The code below is correctly finding the file (I put a showmessage in there to prove it to myself) but the DeleteFile seems to do nothing.
What is wrong with my method?

Code:
if DirectoryExists(Dest) then
      begin
      if FindFirst(Dest + '*.*', faAnyFile, SR) = 0 then
        begin
         repeat
           if (SR.Attr And faDirectory) <> faDirectory then
             begin
             SysUtils.DeleteFile(SR.Name);
             end;
        until FindNext(SR) <> 0;
        FindClose(SR);
        end;
      RemoveDir(Dest);
      end;
 
Several of us were doing directory sweep deletes about 3 weeks back or so.

thread102-1432429

DeleteFile returns a boolean which indicates whether the file is deleted or not. If the file is not deleted, then this is likely returning false. My guess is it has an attribute on it which is making the system refuse to get rid of the file.
 
To look at your code more directly (and you will see these in the example on that thread):

1) You don't filter for directory placeholders ('.', '..').
2) Have you checked what "Dest" holds. I notice if it does not have a '\' as its last character, your code will fail.
 
The problem is (probably) with your SysUtils.DeleteFile statement. You are not specifying the Directory path. The statement should be:
Code:
SysUtils.DeleteFile( [b]Dest + [/b]SR.Name );

It is usually a good idea to ensure that the path ends with a '\'. This is best done using the function IncludeTrailingPathDelimiter so your code could look like this
Code:
if DirectoryExists(Dest) then begin
  Dest := IncludeTrailingPathDelimiter( Dest );
  if FindFirst(Dest + '*.*', faAnyFile, SR) = 0 then begin
    repeat
      if (SR.Attr And faDirectory) <> faDirectory then
        SysUtils.DeleteFile(Dest + SR.Name); 
    until FindNext(SR) <> 0;
    FindClose(SR);
  end;
  RemoveDir(Dest);
end;

Andrew
Hampshire, UK
 
Hi all,
Thanks for the replies.
My apologies to all for not pasting the line directly above my first If statement, as that line carried the includetrailingpathdelimiter statement.

Adding Dest into the deletefile command sorted me out. I had been concentrating so much on ensuring I was definately getting the filename right, it never crossed my mind about the path to it. Doh! Thanks Andy.

Glen, thanks for pointing me to the other thread, and I will be extracting your lines to do with the file attributes, and experimenting with deleting files with various combinations set.
One point tho Glen.
I have found that this line:
Code:
if (SR.Attr And faDirectory) <> faDirectory then
does deal with the [.] & [..] directory placeholders. With it I only get the single file I put in the folder come up as a hit, whereas with this line which I started with
Code:
if (SR.Attr <> faDirectory) then
did come up with the [.] & [..] placeholders.
(I got the tweaked line from whosrdaddy)
Hope that is of use..

Steve
 
Glen, thanks for pointing me to the other thread, and I will be extracting your lines to do with the file attributes, and experimenting with deleting files with various combinations set.

I have the "cleaned up" code in front of me now and I can go ahead and give you that (much easier than what was originally posted):

Code:
atribute := 0;
FileSetAttr(filepath + '\' + tfileinfo.name, atribute);
if DeleteFile(filepath + '\' + tfileinfo.name) = false then
  writeln('Error deleting ', filepath + '\' + tfileinfo.name);

Code:
atribute := FaDirectory;
FileSetAttr(filepath, atribute);
if RemoveDir(filepath) = false then
  writeln('Error removing ', filepath);

As for the placeholder thing, I was thinking more your input parms than how the other code is (remember it takes out the whole tree not just one directory). An input parm of '..' in Dest shouldn't be much trouble, but might be a bit unexpected, especially if you have this code within a procedure/function that someone else might call.

A question for you to think about, though: What are you going to do within this code if you run it against a directory which has subdirectories? RemoveDir will fail if that is the case, as well. You didn't state your requirements fully, so I don't know that answer, but it's something for you to think about.
 
Hi Glen,

Thanks for the updated code.

Your question is spot on. I've been taking the one step at a time approach, whilst it's not the most efficient, until I have a better grasp on things, I like to work that way and build up my knowledge base.
I do indeed want to cover the eventuallity of sub folders in the target folder. My requirement is to completely clear out the target folder.
In reality, for this application, it's unlikely there will be sub folders, but obviously it's good practice for me to cater for it.

Looking at your procedure m_deltree in the thread mentioned above, am I correct in deducing the procedure is calling itself in this line:
Code:
if (tfileinfo.attr and faDirectory = faDirectory) then
            begin
            if (tfileinfo.name <> '.') and (tfileinfo.name <> '..') then
                m_deltree(filepath + '\' + tfileinfo.name)
            end
          else
<snip>

Which is 'if the file found is a folder and not [.] or [..] then call the procedure to delete that folder and files within'.
Seems to be exactly what I'm looking for if so.

Steve

BTW, HNY2008! I'm off now to wipe out random invaluable information from my brain's FAT...
 
am I correct in deducing the procedure is calling itself in this line

Yes. When you do that, though, you want to watch for stack space errors, but that's seemingly rare within Delphi with the amount of stack that is allocated to most apps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top