Pause a File Search

eNarc
eNarc used Ask the Experts™
on
Hi, how do I search a folder and sub folders saving the path to a Stringlist.

when the file count reaches 1000 it will save the SL to a file and clears the SL back to 0 and carries on searching through the files.

the only problem that I'm having is that when its saving the sub directrices are still active and still searching, and it doesn't collect all the files.

as the file count in the files is not the file count that was counted so when its saving its missing files.

this is what I'm using to search, though how do I add the pause all the searches and when the save is complete it resumes all the searches

function BeginSearch(Path:string):string;
var
  sr: TSearchRec;
begin
  Path:=IncludeTrailingBackSlash(Path);
  if FindFirst(Path + '*.*', faAnyFile, sr) = 0 then begin
    repeat
      Application.ProcessMessages;
      if (sr.Name <> '.') and (sr.Name <> '..') then begin
        if (sr.Attr and faDirectory)>0 then begin
          BeginSearch(Path+sr.Name);
        end else begin
          FileSL.add(Path+sr.Name);            
      end;
      end;       
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;
end;
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Engineer
Commented:
You can just add the code directly in your search loop
function BeginSearch(Path:string):string;
var
  sr: TSearchRec;
begin
  Path:=IncludeTrailingBackSlash(Path);
  if FindFirst(Path + '*.*', faAnyFile, sr) = 0 then begin
    repeat
      Application.ProcessMessages;
      if (sr.Name <> '.') and (sr.Name <> '..') then begin
        if (sr.Attr and faDirectory)>0 then begin
          BeginSearch(Path+sr.Name);
        end else 
        begin       
          FileSL.add(Path+sr.Name);  
          if FileSL.Count >= 1000 then
            FileSL.SaveToFile('c:\myfile_' + IntToStr(GetTickCount) + '.txt');            
        end;
      end;       
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;
end;

Open in new window

Author

Commented:
though it doesn't collect all the files as while its saving the other subfiles are being searched at the same time I need to be able to pause all searches while it saves and then once its done continues as if it wasn't paused.
Mike McCrackenSenior Consultant
Most Valuable Expert 2011
Top Expert 2013
Commented:
Do you have multiple threads running?
If so you will have to coordinate with them to stop when 1000 is reached.

mlmcc
Commented:
I searched for folders then searched a folder one by one for files that way they all are in 1 thread and it works.

Author

Commented:
good

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial