AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Search for multiple files at once in PC

Ein Thema von danten · begonnen am 31. Dez 2012 · letzter Beitrag vom 2. Jan 2013
Antwort Antwort
danten

Registriert seit: 19. Feb 2012
Ort: Czech Republic, Prag
126 Beiträge
 
Delphi 10.1 Berlin Architect
 
#1

Search for multiple files at once in PC

  Alt 31. Dez 2012, 15:31
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 := 'Ato 'Zdo
  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.
Daniel
  Mit Zitat antworten Zitat
Furtbichler
(Gast)

n/a Beiträge
 
#2

AW: Search for multiple files at once in PC

  Alt 1. Jan 2013, 11:41
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
  Mit Zitat antworten Zitat
danten

Registriert seit: 19. Feb 2012
Ort: Czech Republic, Prag
126 Beiträge
 
Delphi 10.1 Berlin Architect
 
#3

AW: Search for multiple files at once in PC

  Alt 1. Jan 2013, 12:34
Thank you, can you please give some code.
Daniel
Daniel
  Mit Zitat antworten Zitat
delnu
(Gast)

n/a Beiträge
 
#4

AW: Search for multiple files at once in PC

  Alt 1. Jan 2013, 14:38
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 ) ...

Geändert von delnu ( 1. Jan 2013 um 14:48 Uhr) Grund: edit
  Mit Zitat antworten Zitat
Furtbichler
(Gast)

n/a Beiträge
 
#5

AW: Search for multiple files at once in PC

  Alt 1. Jan 2013, 15:40
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.
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.540 Beiträge
 
Delphi 11 Alexandria
 
#6

AW: Search for multiple files at once in PC

  Alt 2. Jan 2013, 07:29
Maybe you want to give my component a try.
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 02:07 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