Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5943005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:20:00+00:00 2026-05-22T16:20:00+00:00

Got this pretty straight forward function to search for files: function FindFiles(const Path, Mask:

  • 0

Got this pretty straight forward function to search for files:

function FindFiles(const Path, Mask: string; IncludeSubDir: boolean): integer;
var
  FindResult: integer;
  SearchRec: TSearchRec;
begin
  Result := 0;
  FindResult := FindFirst(Path + Mask, faAnyFile - faDirectory, SearchRec);
  while FindResult = 0 do
  begin
    //!!!!!!!! This must synchronize Form1.Memo2.Lines.Add(Path + SearchRec.Name);
    Result := Result + 1;
    FindResult := FindNext(SearchRec);
  end;
  FindClose(SearchRec);
  if not IncludeSubDir then
    Exit;
  FindResult := FindFirst(Path + '*.*', faDirectory, SearchRec);
  while FindResult = 0 do
  begin
    if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
      Result := Result + FindFiles(Path + SearchRec.Name + '\', Mask, True);
      FindResult := FindNext(SearchRec);
  end;
  FindClose(SearchRec);
end;

It is called with :

FindFiles('C:\','*.*',TRUE)

How to break this into Delphi thread?
This code suits my needs (d2010) I just need to put it (or parts of it) into a thread.
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:20:01+00:00Added an answer on May 22, 2026 at 4:20 pm

    Maybe something like this?

    unit Unit2;
    
    interface
    
    uses
      SysUtils, Classes;
    
    type
      TFileSearcher = class(TThread)
      private
        { Private declarations }
        FPath, FMask: string;
        FIncludeSubDir: boolean;
        FItems: TStrings;
        function FindFiles: integer;
        procedure UpdateTheMemo;
      public
        constructor Create(CreateSuspended: boolean; const Path, Mask: string; IncludeSubDir: boolean);
      protected
        procedure Execute; override;
      end;
    
    implementation
    
    uses Unit1;
    
    { TFileSearcher }
    
    constructor TFileSearcher.Create(CreateSuspended: boolean; const Path, Mask: string;
      IncludeSubDir: boolean);
    begin
      inherited Create(CreateSuspended);
      FPath := Path;
      FMask := Mask;
      FIncludeSubDir := IncludeSubDir;
    end;
    
    procedure TFileSearcher.Execute;
    begin
      FItems := TStringList.Create;
      try
        FindFiles;
        Synchronize(UpdateTheMemo);
      finally
        FItems.Free;
      end;
    end;
    
    procedure TFileSearcher.UpdateTheMemo;
    begin
      Form1.Memo2.Lines.Assign(FItems);
    end;
    
    function TFileSearcher.FindFiles: integer;
    var
      FindResult: integer;
      SearchRec: TSearchRec;
      ThisPath: string;
    begin
      ThisPath := FPath;
      Result := 0;
      FindResult := FindFirst(FPath + FMask, faAnyFile - faDirectory, SearchRec);
      while FindResult = 0 do
      begin
        FItems.Add(FPath + SearchRec.Name);
        Result := Result + 1;
        FindResult := FindNext(SearchRec);
      end;
      FindClose(SearchRec);
      if not FIncludeSubDir then
        Exit;
      FindResult := FindFirst(IncludeTrailingBackslash(ThisPath) + '*.*', faDirectory, SearchRec);
      while FindResult = 0 do
      begin
        if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
        begin
          FPath := IncludeTrailingBackslash(ThisPath + SearchRec.Name);
          FIncludeSubDir := true;
          Result := Result + FindFiles();
        end;
        FindResult := FindNext(SearchRec);
      end;
      FindClose(SearchRec);
    end;
    
    end.
    

    If you want the items to be added to the VCL control one-by-one you lose some of the benefits of threading, but sure, it can be done:

    unit Unit2;
    
    interface
    
    uses
      SysUtils, Classes;
    
    type
      TFileSearcher = class(TThread)
      private
        { Private declarations }
        FPath, FMask: string;
        FIncludeSubDir: boolean;
        FItemToAdd: string;
        function FindFiles: integer;
        procedure UpdateTheMemo;
      public
        constructor Create(CreateSuspended: boolean; const Path, Mask: string; IncludeSubDir: boolean);
      protected
        procedure Execute; override;
      end;
    
    implementation
    
    uses Unit1;
    
    { TFileSearcher }
    
    
    constructor TFileSearcher.Create(CreateSuspended: boolean; const Path, Mask: string;
      IncludeSubDir: boolean);
    begin
      inherited Create(CreateSuspended);
      FPath := Path;
      FMask := Mask;
      FIncludeSubDir := IncludeSubDir;
    end;
    
    procedure TFileSearcher.Execute;
    begin
      FindFiles;
    end;
    
    procedure TFileSearcher.UpdateTheMemo;
    begin
      Form1.Memo2.Lines.Add(FItemToAdd);
    end;
    
    function TFileSearcher.FindFiles: integer;
    var
      FindResult: integer;
      SearchRec: TSearchRec;
      ThisPath: string;
    begin
      ThisPath := FPath;
      Result := 0;
      FindResult := FindFirst(FPath + FMask, faAnyFile and not faDirectory, SearchRec);
      while FindResult = 0 do
      begin
        FItemToAdd := FPath + SearchRec.Name;
        Synchronize(UpdateTheMemo);
        Result := Result + 1;
        FindResult := FindNext(SearchRec);
      end;
      FindClose(SearchRec);
      if not FIncludeSubDir then
        Exit;
      FindResult := FindFirst(IncludeTrailingBackslash(ThisPath) + '*.*', faDirectory, SearchRec);
      while FindResult = 0 do
      begin
        if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
        begin
          FPath := IncludeTrailingBackslash(ThisPath + SearchRec.Name);
          FIncludeSubDir := true;
          Result := Result + FindFiles();
        end;
        FindResult := FindNext(SearchRec);
      end;
      FindClose(SearchRec);
    end;
    
    end.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems pretty straight forward but I'm not sure what is going wrong. I'm
this is probably pretty simple, but I've got this text file containing a bunch
I'm stuck with this pretty silly thing; I got a textfile like this; Hello::140.0::Bye
I've got the following situation: <h2>This text is <span>pretty awesome</span></h2> I'm trying to give
So! My question is pretty straight forward, I have a website (Build in ASP.NET
I've got a string like this: #################### Section One #################### Data A Data B
Got this line of code here but its not working. private void Button_Click(object sender,
Got this site with UN/PW set via the Createuserwizard control. Client considers PW too
Got this: Table a ID RelatedBs 1 NULL 2 NULL Table b AID ID
Got this code for a viewscroller from the apple developers site. @synthesize scrollView1, scrollView2;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.