Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Algorithmen, Datenstrukturen und Klassendesign (https://www.delphipraxis.net/78-algorithmen-datenstrukturen-und-klassendesign/)
-   -   Delphi Search for multiple files at once in PC (https://www.delphipraxis.net/172396-search-multiple-files-once-pc.html)

danten 31. Dez 2012 15:31

Search for multiple files at once in PC
 
Hi all,
please help.
How do I write a function to search for multiple files on your computer at once.
I need to find files:
abcd.exe, 1234.ini, GHIJ.BAT, 0987.dat, tinyplay.exe !!
I use to find one set this feature:
Delphi-Quellcode:
function Tfindplayer.ScanL7(root, filemask: string; hitlist: String): Boolean;
  function ScanDirectory(var path: string): Boolean;
  var
    SRec: TSearchRec;
    pathlen: Integer;
    res: Integer;
  begin
    pathlen := Length(path);
    { first pass, files }
    res := FindFirst(path + filemask, faAnyfile+faHidden, SRec);
    If (FileExists(path+'tinyplay.exe') = True) then
      begin
        FScanAborted := True;
        sLabel24.Caption := ExtractShortPathName(ExcludeTrailingBackslash(sLabel24.Caption));
        sProgressBar1.Position := sProgressBar1.Position + 10;
        image12.Picture.Assign(x1);
        Exit;
      end;
    if res = 0 then
      try
        while res = 0 do
        begin
          hitlist := (path + SRec.Name);
          res := FindNext(SRec);
        end;
      finally
        FindClose(SRec);
      end;
    Application.ProcessMessages;
    Result := not (FScanAborted or Application.Terminated);
    if not Result then Exit;

    {second pass, directories}
    res := FindFirst(path + '*.*', faDirectory+faHidden, SRec);
    if res = 0 then
      try
        while (res = 0) and Result do
        begin
          if ((Srec.Attr and (faReadOnly or faHidden or faSysFile or faDirectory)) <> 0) and
            (Srec.Name <> '.') and
            (Srec.Name <> '..') then
          begin
            path := path + SRec.Name + '\';
            Result := ScanDirectory(path);
            SetLength(path, pathlen);
          end;
          res := FindNext(SRec);
        end;
      finally
        FindClose(SRec);
      end;
  end;

begin
  FScanAborted := False;
  Screen.Cursor := crHourglass;
  try
    Result := ScanDirectory(root);
  finally
    Screen.Cursor := crDefault
  end;
end;

function Tfindplayer.L7:boolean;
var
  Item: string;
  i: integer;
  ch: Char;
  root: string;
begin
  root := 'C:\';
  for ch := 'A' to 'Z' do
  begin
    root[1] := ch;
    case GetDriveType(PChar(root)) of
      DRIVE_FIXED, DRIVE_REMOTE:
        if not ScanL7(root,'tinyplay.exe', sLabel22.Caption) then
          Break;
    end;
  end;
end;
Thanks all.

Furtbichler 1. Jan 2013 11:41

AW: Search for multiple files at once in PC
 
1. Load all filenames from directory using FindFirst/FindNext/FindClose
1.1 If a filename matches your list of files => found it
2. Do the same with all directories in your directory:
2.1 let PATH be the path to the directory you are currently searching in. Let SearchRec.Name be the name of a subdirectory. Then simply call yourself with PATH+'\'+SearchRec.Name

danten 1. Jan 2013 12:34

AW: Search for multiple files at once in PC
 
Thank you, can you please give some code.
Daniel

delnu 1. Jan 2013 14:38

AW: Search for multiple files at once in PC
 
Maybe that this code not exactly feed your needs, but I used it in a big program to check if some of the filetypes are found in the given path.

In a checklistbox I gave some names of filetypes, then I let the program search for it in a given path. If same type matches in this directory, this type is checked in the checklistbox. Instead of using the checkboxes you may use boolean variables that shows, if the searched file/fileytpe was found.
Delphi-Quellcode:
PROCEDURE FindTypes(s:string); // "s" ist the path to search in
VAR
 cnt: integer;
 pfad,
 n: string;
 sr: TSearchRec;

 PROCEDURE SUBB;
 BEGIN
  WITH Form1 DO BEGIN
   n:=AnsiUpperCase(ExtractFileExt(sr.name));
   IF  n='.BMP'
   THEN CheckListBox1.checked[ 0]:=TRUE;
   IF  n='.GIF'
   THEN CheckListBox1.checked[ 1]:=TRUE;
   IF  n='.ICO'
   THEN CheckListBox1.checked[ 2]:=TRUE;
   IF  n='.JPG'
   THEN CheckListBox1.checked[ 3]:=TRUE;
   IF  n='.PCX'
   THEN CheckListBox1.checked[ 4]:=TRUE;
   IF  n='.PNG'
   THEN CheckListBox1.checked[ 5]:=TRUE;
   IF  n='.TIF'
   THEN CheckListBox1.checked[ 6]:=TRUE;
   IF  n='.WMF'
   THEN CheckListBox1.checked[ 7]:=TRUE;
  END;
 END; { PROCEDURE SUBB }

BEGIN { PROCEDURE FindTypes }
 WITH Form1 DO BEGIN
  FOR cnt:=0 TO CheckListBox1.Items.count-1 DO CheckListBox1.checked[cnt]:=FALSE;
  IF  DirectoryExists(s)
  THEN pfad:=s
  ELSE pfad:=ExtractFilePath(s);
  IF pfad[length(pfad)]<>'\' THEN pfad:=pfad+'\';
{ showmessage(pfad+^J^M+s);}
  IF  autoFind
  THEN BEGIN
   IF  FindFirst(pfad+'*.*',faAnyFile,sr)=0
   THEN BEGIN
    WHILE FindNext(sr) = 0
    DO   BEGIN
     subb;
    END;
    FindClose(sr);
   END;
  END ELSE BEGIN
   IF  FindFirst(pfad+'*.*',faAnyFile,sr)=0
   THEN BEGIN
    WHILE FindNext(sr) = 0
    DO  IF AnsiUpperCase(sr.name)=extractFileName(s)
    THEN BEGIN
     subb;
    END;
    FindClose(sr);
   END;
  END;
//SetPicTypes;
 END;
END; { PROCEDURE FindTypes }
edit :
Just I don't know, what the boolean variable "autofind" was. I wrote the program several years ago and just took that bit of the code.

Instead of the checklistbox you can use a listbox with files to search for and then put the results into another listbox with the whole result ( drive + path + filename ) ...

Furtbichler 1. Jan 2013 15:40

AW: Search for multiple files at once in PC
 
Zitat:

Zitat von danten (Beitrag 1197279)
Thank you, can you please give some code.

Why should I?

Ok, It's new year.

Delphi-Quellcode:
Procedure FindFiles (path : String; Files : TStrings; FoundFiles : TSTrings);
Var
  S : TSearchRec;

Begin
  if FindFirst(path+'\*.*',faAnyFile,S)=0 then begin
    repeat
      if S.Attr and faDirectory<>0 then
        FindFiles(path+'\'+s.Name,Files,FoundFiles)
      else if MatchesAny(S.Name, Files) then
        FoundFiles.Add(path+'\'+S.Name);
    until FindNext(S)<>0;
    FindClose(S);
  end;

    if FindFirst(path+'\*',faDirectory ,S)=0 then begin
    repeat
      if (s.Name<>'.') and (s.Name<>'..') Then
        FindFiles(path+'\'+s.Name,Files,FoundFiles)
    until FindNext(S)<>0;
    FindClose(S);
  end;
End;
The function 'MatchesAny' tries to match a filename with a list of given files (including wildcards). Maybe you can code this yourself.

DeddyH 2. Jan 2013 07:29

AW: Search for multiple files at once in PC
 
Maybe you want to give my component a try.


Alle Zeitangaben in WEZ +1. Es ist jetzt 11:23 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz