Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I have small app (was written by Delphi 7) to delete a folder (include sub folder and children files).
Main code of this app is below method:

Delphi
procedure TForm1.DeleteDirectory(const Name: string);
var
  F: TSearchRec;
begin
  //Delete image folder
  if FindFirst(Name + '\*', faAnyFile, F) = 0 then begin
    try
      repeat
        if (F.Attr and faDirectory <> 0) then begin
          if (F.Name <> '.') and (F.Name <> '..') then begin
            DeleteDirectory(Name + '\' + F.Name);
          end;
        end else begin
          DeleteFile(Name + '\' + F.Name);
        end;
      until FindNext(F) <> 0;
      RemoveDir(Name);
    finally
      FindClose(F);
    end;
  end;
end;

Problem:
When I run app in Window 7, selected folder (folder name is input param of method) was deleted (all children files, sub folder and main folder were deleted).
However, if i run app in window XP, all children files were deleted but folders (sub folder and main folder cannot be deleted).
I have solution for this problem, but i want to know why, why program can delete folders on window 7 but cannot delete folders on window XP?

Thanks for your support!


What I have tried:

I have solution, i want know cause of problem
Posted
Updated 3-May-16 0:51am
Comments
Jochen Arndt 3-May-16 4:30am    
Check the return value of RemoveDir() and use getlasterror if it fails to know why the folder is not deleted. You should do this too when deleting a file because when a file can't be deleted the directory is not empty and removing it will fail.

While I don't think that this is the reason you may also call RemoveDir() after closing the find handle.
ThanhNT14 3-May-16 4:46am    
Cannot delete folder because folders are holding by TSearchRec object (F object).
If move RemoveDir() after close the find handle, problem will be solved.
I know that, but i don't understand why problem only occur on window XP, does not occur on window 7?
Jochen Arndt 3-May-16 5:14am    
Use the Reply button right of a poster's name. Then those will get an email notification. So I saw your comment by chance.

I don't know the exact answer but it seems that the XP Find functions will lock the directory until FindClose is called so that it can't be deleted and this has been fixed with later Windows versions.
ThanhNT14 3-May-16 6:18am    
Thanks Jochen Arndt.
I also don't know exact reason, so i need your support.
Jochen Arndt 3-May-16 6:52am    
I have posted a solution the get the question answered.

However, only Microsoft would be able to answer why it behaves so with XP and what has been changed with newer Windows versions.

1 solution

It seems that with Windows XP the file search functions (Windows API functions FindFirstFile, FindFirstFileEx) will lock the directory.

As a result the directory which is searched can't be deleted while the search handle has not been closed (like a file that is actually opened can't be deleted).

It seems also that this behaviour has been changed with newer Windows versions.

My google-fu seems to be not very strong today but I found at least one discussion where this has been observed too (Google Groups[^]):
Quote:
..seems that on XP, FindFirstFile locks the directory, while on
Win7 it does not.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900