Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (3)


How to check if a given folder is emptyFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
Files Operation
Skill Level:
Scoring:
Last Update:
03/11/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet isemptyfolder filectrl findfirst findnext findclose
Times Scored:
6
Visits:
3201
Uploader: Christian Cristofori
Company: Function2 by C. Cristofori
Reference: N/A
 
Question/Problem/Abstract:
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 benchmarked yet.
Answer:



You'll need to add the "FileCtrl" and "SysUtils" units in your uses clausole.

uses
  FileCtrl, SysUtils;

and then declaring this, the function you need.

function IsEmptyFolder(fld:string):boolean;
var
  sr:tsearchrec;
  r:integer;
begin
  fld:=IncludeTrailingBackSlash(fld);
  result:=false;
  if(DirectoryExists(fld))then
  begin
    result:=true;
    r:=findfirst((fld+'*.*'),faAnyFile,sr);
    while((r=0)and(result))do
    begin
      // Revision 2:
      // checks for system folders "." and ".." that always exists
      // inside an empty folder.
      if((SR.Attr and faDirectory)<>0)then
      begin
        if((sr.name<>'.')and(sr.name<>'..'))then
        begin
          result:=false;
        end;
      end else
      begin
        result:=false;
      end;
      r:=findnext(sr);
    end;
    // Revision 1:
    // this prevents compiler by using the API defined in windows unit,
    // that will raise a compiler error like this:
    // [Error]:Incompatible types: 'Cardinal' and 'TSearchRec'
    sysutils.findclose(sr);
  end;
end;

The function only requires one parameter: the folder you want to check. It returns "True" if the folder is empty or "False" if the folder contains something.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
How to check if a given folder is empty
    Hugo Javier Sanoguera (Mar 13 2002 12:36PM)

My apologize to the author. El function has been modified here!.
We changed the firs result := false to raise an exception, too.
Sorry, but I recent say me the change.
Respond

How to check if a given folder is empty
    Hugo Javier Sanoguera (Mar 13 2002 12:33PM)

The function written by the author is very slowly in Windows, System32 or other folder containing much files. The function will must exit when a file is found in the folder. So, Why advance until last item?
Respond

RE: How to check if a given folder is empty
Christian Cristofori (Mar 20 2002 10:10AM)

The function exits when a file is found: just look at the while condition "...and result)".
I tested on my c:\winnt\system32 folder and it's very fast.
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
C.A. Longen
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)