Delphi Multi-threaded file Search

Source: Internet
Author: User

File search function to use the FindFirst and FindNext functions, as well as TSEARCHREC results, the use of depth-first algorithm, first search directory files, and then search the directory of subdirectories recursive call, the code is as follows  procedure Tsearchthread.findfiles (apath:string);     {with recursive invocation, you can find files in the specified format in the current directory and subdirectories}var     fsearchrec,dsearchrec:tsearchrec;    findresult:integer;begin        apath : =getdirectoryname (Apath); Get a valid directory name          {Find a matching file}        findresult:= FindFirst (Apath+ffilename,faanyfile+fahidden+fasysfile+fareadonly,fsearchrec);         try        {continue to find matching files}        while findresult=0 do        begin            form1.listbox1.items.add (Apath + fsearchrec. Name);            //filecopy (Apath+fsearchrec. Name, LocalPath + Fsearchrec. Name);            findresult:=findnext (FSEARCHREC);        end;         {Find in subdirectories of current directory}        //Search directory under current directory         findresult:=findfirst (apath+ ' *. * ', fadirectory, DSEARCHREC);        while findresult=0 do        begin   & nbsp        if (Dsearchrec. Attr and Fadirectory) =fadirectory) and not                isdirnotation (Dsea Rchrec. Name) then            findfiles (Apath+dsearchrec. Name);//Here is a recursive call             findresult:=findnext (DSEARCHREC);     & nbsp  end;        finally            findclose (FSEARCHREC) ;    end;end;  also implements a hard drive search, uses the Drivecombobox control, then determines the drive type, and does not do search objects for floppy disks and discs. The code is as follows      for i:=0 to Form1.drive.items.count-1 do    begin        c: = Copy (Form1.Drive.Items, 0, 2);        C : = C + ' \ ';        dtype: = GetDriveType (PChar (C));        if (DType = drive_fixed) then            findfiles (C);    end;  The last need to explain is the implementation of multithreading. If a hard drive is found under MainForm to start a thread, it will produce multiple threads, bad control, such as pressing the Stop button to terminate these threads, so consider putting the above loop judgment on the thread. This way, when it executes the findfiles, it can block the wait.   issues to be solved 1. How to search for multiple files 2. If you put the loop to determine the type of drive in MainForm, start the search thread and then how to block (WinExec I think is the start from the line program, but there is a way to block to its termination)

Delphi Multi-threaded file Search

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.