Recent

Author Topic: FindAll Files questions  (Read 2544 times)

KarenT

  • Full Member
  • ***
  • Posts: 120
FindAll Files questions
« on: July 20, 2017, 11:59:04 pm »
Hello,

I want to use FindAllFiles in a loop of Folders, but because it creates the StringList itself, it seems to only keep the last iteration, "E:\test" in the following pseudo-code. I'd like the slFiles to have all three results combined.

Code: Pascal  [Select][+][-]
  1. repeat
  2.   slFiles:=FindAllFiles('C:\test','*.txt',True);
  3.   slFiles:=FindAllFiles('E:\test','*.txt',True);
  4.   slFiles:=FindAllFiles('F:\test','*.txt',True);
  5. until...
  6.  

1: Is there some way to override the TStringList creation?
I know I can amalgamate them all into a List box or something, but just asking first. :)

2: Is there a CallBack version anywhere that can feed a StatusBar in a lengthy process?

Thank you.

carl_caulkett

  • Sr. Member
  • ****
  • Posts: 306
Re: FindAll Files questions
« Reply #1 on: July 21, 2017, 12:49:44 am »
Hi Karen,

How about:

Code: Pascal  [Select][+][-]
  1. var
  2.   slFiles: TStrings;
  3. begin
  4.   repeat
  5.     slFiles := TStringList.Create;
  6.     try
  7.       slFiles.AddStrings(FindAllFiles('C:\test','*.txt',True));
  8.       slFiles.AddStrings(FindAllFiles('E:\test','*.txt',True));
  9.       slFiles.AddStrings(FindAllFiles('F:\test','*.txt',True));
  10.       ...
  11.       // Do something with slFiles
  12.       ...
  13.     finally
  14.       slFile.Free;
  15.     end;
  16.   until...
  17. end;
  18.  
"It builds... ship it!"

Mac Mini M1
macOS 13.6 Ventura
Lazarus 2.2.6 (release version)
FPC 3.2.2 (release version)

carl_caulkett

  • Sr. Member
  • ****
  • Posts: 306
Re: FindAll Files questions
« Reply #2 on: July 21, 2017, 12:57:01 am »
I'm a bit puzzled as to why the repeat..until is there, by the way. I expect if I saw the full code it would be more obvious.
"It builds... ship it!"

Mac Mini M1
macOS 13.6 Ventura
Lazarus 2.2.6 (release version)
FPC 3.2.2 (release version)

wp

  • Hero Member
  • *****
  • Posts: 11833
Re: FindAll Files questions
« Reply #3 on: July 21, 2017, 12:57:58 am »
No don't do this, it will create a memory leak because the instances of the Stringlist created by FindAllFiles will never be destroyed. I never liked this construction.

Instead, use the overloaded version of FindAllFiles which has the StringList as the first parameter. The StringList is not created by FindAllFiles any more.

Code: Pascal  [Select][+][-]
  1. var
  2.   slFiles: TStrings;
  3. begin
  4.    slFiles := TStringList.Create;
  5.    try
  6.       FindAllFiles(slFiles, 'C:\test','*.txt',True);
  7.       FindAllFiles(slFiles, 'E:\test','*.txt',True);
  8.       FindAllFiles(slFiles, 'F:\test','*.txt',True);
  9.       ...
  10.       // Do something with slFiles
  11.       ...
  12.    finally
  13.       slFiles.Free;
  14.    end;
  15. end;
« Last Edit: July 21, 2017, 09:10:54 am by wp »

carl_caulkett

  • Sr. Member
  • ****
  • Posts: 306
Re: FindAll Files questions
« Reply #4 on: July 21, 2017, 01:04:30 am »
Good catch, wp! I shouldn't try to answer questions this late in the evening. I wasn't aware that there was a FindAllFiles method. That could be useful!
"It builds... ship it!"

Mac Mini M1
macOS 13.6 Ventura
Lazarus 2.2.6 (release version)
FPC 3.2.2 (release version)

KarenT

  • Full Member
  • ***
  • Posts: 120
Re: FindAll Files questions
« Reply #5 on: July 21, 2017, 10:27:12 pm »
No don't do this, it will create a memory leak because the instances of the Stringlist created by FindAllFiles will never be destroyed. I never liked this construction.

Thanks I didn't like the idea of it either. Not an expert by any stretch but taking away from the Programmer, the Creation, but leaving the Free for them seemed very weird. But, that's how they showed an example in the onllne Help wiki.

That has solved the problem nicely, thank you.


wp

  • Hero Member
  • *****
  • Posts: 11833
Re: FindAll Files questions
« Reply #6 on: July 21, 2017, 11:05:16 pm »
Which wiki article do you mean? If found this one (http://wiki.freepascal.org/FindAllFiles), updated it with the overloaded procedure and added a warning of the memory leak risk.

KarenT

  • Full Member
  • ***
  • Posts: 120
Re: FindAll Files questions
« Reply #7 on: July 22, 2017, 01:20:48 am »
Which wiki article do you mean? If found this one (http://wiki.freepascal.org/FindAllFiles), updated it with the overloaded procedure and added a warning of the memory leak risk.

Yes, that is the one I started out with. Could you re-write the example as most people are likely to grab the code and run. It was only because I needed to amalgamate stuff that I found the problem and then asked here.

 

TinyPortal © 2005-2018