#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Location
    South Africa - Johannesburg
    Posts
    2
    Rep Power
    0

    Counting number of files in a directory


    Hi people. Probably a simple thing. I need to count the number of files in a directory and then output that to a text file. I can do the text file part. Not sure about the counting the files. Please help.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    You can use FindFirst, FindNext and FindClose to iterate through a directory. You can count the files while iterating thru the dir. See the example for FindFirst in the help file for how to use these functions.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Location
    South Africa - Johannesburg
    Posts
    2
    Rep Power
    0

    ??


    I have looked at the helpfiles and it isnt very clear. The example is checking file attributes. Please will you give me abit more details. Thanks
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    The following code evaluates all the files in C:\*.* and excludes directories from that file count.
    Code:
    function TForm1.CountFiles : integer;
    var
    	Rec : TSearchRec;
    	nFileCount : integer;
    	foo : string;
    begin
    	nFileCount := 0;
    	if FindFirst('C:\*.*', faAnyFile, Rec) = 0 then
    	begin
    		repeat
    			// Exclude directories from the list of files.
    			if ((Rec.Attr and faDirectory) <> faDirectory) then
    				Inc(nFileCount);
    		until FindNext(Rec) <> 0;
    		FindClose(Rec);
    	end;
    	Result := nFileCount;
    end;
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo