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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting files while omitting what has already been counted

Status
Not open for further replies.

tarja311

Programmer
Jan 23, 2007
7
US
Hello all,

I have a function within a thread that counts recursively the number of files there are within a given directory or directories and its subdirs. These directories are stored inside a TStringList, and I have a for-loop that iterates through the list, counting the number of files within them. This works fine.

My problem is that every time I add a new directory to the list, the for-loop iterates throughout the entire thing. From the first dir to the last dir. This can be really time-consuming considering I am counting thousands of files within each directory.

What I really want it to do is to not re-count the old directories that were already counted, but instead count the newly added directories and increment that total to the total amount.

I am using a really simple approach:
Code:
I     : Integer;
r, RN : Int64;
root  : String;
ROOTS : TStringList;

RN  := 0;
r   := 0;

for I := 0 to ROOTS.Count - 1 do
begin
 root := ROOTS[I];
 RN := FindFiles(root, '*.*', True);
 r := r + RN;
end;

lblSharedTotal.Caption := IntToStr(r) + ' Files Shared';

My search algorithm uses FindFirst and FindNext if that helps any.

Thanks
 
I am sure theres simple answer to this, but why don't you count the file names as you add them?

If the filenames are unique, then you can do a find on the list to make sure you only add new names.





Steve: N.M.N.F.
If something is popular, it must be wrong: Mark Twain
 
There is a really simple answer to this: Don't use a FOR loop. For loops are for when the index and the bounds do not change.

Rewrite to use a WHILE or REPEAT and make sure the control does not do anything like sorting when you add the new directory.

Of course, there probably is a much better way to handle this, in general, but I don't know enough of the OPs requirements to tell.

Measurement is not management.
 
Another option might be to use an ADO connection to the File System and treat the directories as a database table.
 
Thanks guys. I will give it a shot and see what happens.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top