Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to retrieve if a given folder is empty Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
07-Jan-03
Category
Files Operation
Language
Delphi All Versions
Views
112
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Christian Cristofori

Ever needed to check if a folder is empty or not? That's my way of doing. I think 
it's really fast, but not bench marked yet.

Answer:
1   uses
2     FileCtrl, SysUtils;
3   
4   function IsEmptyFolder(fld: string): boolean;
5   var
6     sr: tsearchrec;
7     r: integer;
8   begin
9     fld := IncludeTrailingBackSlash(fld);
10    result := false;
11    if (DirectoryExists(fld)) then
12    begin
13      result := true;
14      r := findfirst((fld + '*.*'), faAnyFile, sr);
15      while ((r = 0) and (result)) do
16      begin
17        // Revision 2:
18        // checks for system folders "." and ".." that always exists
19        // inside an empty folder.
20        if ((SR.Attr and faDirectory) <> 0) then
21        begin
22          if ((sr.name <> '.') and (sr.name <> '..')) then
23            result := false;
24        end
25        else
26          result := false;
27        r := findnext(sr);
28      end;
29      // Revision 1:
30      // this prevents compiler by using the API defined in windows unit,
31      // that will raise a compiler error like this:
32      // [Error]:Incompatible types: 'Cardinal' and 'TSearchRec'
33      sysutils.findclose(sr);
34    end;
35  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC