Announcement

Collapse
No announcement yet.

Schnelle Dateisuche auf Datenträger

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Schnelle Dateisuche auf Datenträger

    Hallo!

    Wie kann man schnellstmöglich prüfen, ob und wo eine bestimmte Datei auf einem Datenträger vorhanden ist? Kann man z.B. direkt die FAT auslesen?

    Danke für Hinweise!

    ([email protected])

  • #2
    Wenn du bereits den kompletten Pfad der Datei weisst, kannst du mit der SysUtils-Routine FileExists überprüfen, ob die Datei existiert. Die Funktion liefert True, wenn die Datei existiert, False, wenn sie nicht vorhanden ist.

    Wenn der Pfad der Datei unbekannt ist, musst du alle Verzeichnisse rekursiv mit den Sysutils-Routinen FindFirst, FindNext und FindClose nach der Datei durchsuchen. Code-Beispiele findest du u.a. in der Delphi-Hilfe.

    Folgende Web-Seiten behandeln direkt das Suchen von Dateien mit FindFirst/FindNext usw:<p>
    http://www.obsti.se/delphiexchange/oracle/q0002.html<p>
    http://www.q3.nu/trucomania/truco.cgi?158&ing<p>

    Gruß,<br>
    Bernhar

    Comment


    • #3
      <p>Hallo Rainer,<br>
      <br>
      um Dateien in Verzeichnissen zu suchen, kannst Du die API-Funktion <b>FindFileInTree</b> benutzen.<br>Damit sparst Du dir die rekursive Suche.<br>
      <br>
      Gruß Thomas<br></p&gt

      Comment


      • #4
        Hm, *wunder, wunder*, von einer <b>FindFileInTree</b>-Funktion habe ich noch nie gehört, obwohl sie recht praktisch wäre. Ich kann diese Funktion auch nirgends finden..

        Comment


        • #5
          <p>Hallo,<br>
          <br>
          die Funktion ist in der Win32-Api-Hilfe dokumentiert.<br>
          Das Delphi-Hilfesystem funktioniert bei Api-Funktionen oft nicht.<br>
          Unter "Symbolleisten - Anpassen - Hilfe" gibt es die Anweisung "Windows-API".<br>
          Die Datei <b>"win32.hlp"</b> befindet sich im Verzeichnis<br>
          <b>"c:\programme\gemeinsame dateien\borland shared\mshelp\"</b><br>
          <br>
          Gruß Thomas<br></p&gt

          Comment


          • #6
            <p>Hallo nochmal,<br>
            <br>
            folgend der Auszug zur Funktion aus der Win-Api-Hilfe:
            <p></p>
            <pre>
            FindFileInTree
            --------------

            LPSTR FindFileInTree(LPSTR szFile, LPSTR szDir, LPSTR szBuf, int cbBuf);

            FindFileInTree searches for the specified file in a directory and its subdirectories.

            Arguments

            szFile: Specifies the name of the file you are trying to locate.

            szDir : Specifies the top-level directory of the tree structure you want to search.

            szBuf : Points to the destination buffer that will receive the path string.

            cbBuf : Length, in bytes, of the destination buffer.

            Return Value

            The return value is the full path of the first instance of the file. If the file isn't found, the return value is an empty string.
            </pre>
            <br>
            Gruß Thomas<br></p&gt

            Comment


            • #7
              <p>Hallo nochmal,<br>
              <br>
              folgend der Auszug zur Funktion aus der Win-Api-Hilfe:
              <pre>
              FindFileInTree
              --------------

              LPSTR FindFileInTree(LPSTR szFile, LPSTR szDir, LPSTR szBuf, int cbBuf);

              FindFileInTree searches for the specified file in a directory and its subdirectories.

              Arguments

              szFile: Specifies the name of the file you are trying to locate.

              szDir : Specifies the top-level directory of the tree structure you want to search.

              szBuf : Points to the destination buffer that will receive the path string.

              cbBuf : Length, in bytes, of the destination buffer.

              Return Value

              The return value is the full path of the first instance of the file. If the file isn't found, the return value is an empty string.
              </pre>
              <br>
              Gruß Thomas<br></p&gt

              Comment


              • #8
                Hallo Thomas,<br>die Funktion ist zwar dokumentiert, wird aber von Borland nicht importiert. D.h. selber importieren. Nur weiß ich nicht in welcher dll ich nach FindFileInTree suchen soll.<br>:-) Jens Schuman

                Comment


                • #9
                  Der Aufruf FindFileInTree ist kein API-Aufruf.<br>
                  Es handelt sich also um einen Fehler der API-Hilfe.<br>
                  Im MSDN wird er noch nicht mal erwähnt.<br>
                  Zu finden ist er in der Datei MSSETUP.DLL im MS Visual Studio.<br>
                  Den Aufruf habe ich aber bisher nicht realisieren können.<br&gt

                  Comment


                  • #10
                    Hallo Zusammen,<br>
                    <br>
                    für FindFileInTree ist in der Header Datei MSDetect.H deklariert, und soll sich<br>
                    in der DLL MSDETSTF.DLL befinden<br>
                    (siehe Include Files im SDK).<br>
                    Zumindest war das die einzige Stelle wo ich es finden konnte.<br>
                    Die MSSETUP.DLL (Version 3.01) konnte ich im Verzeichis:<br>
                    C:\Programme\Internet Explorer\Setup<br>
                    wer einen IE 5 hat, sollte sie also eigentlich haben.<br>
                    Die Funktion ist übrigens falsch Dokumentiert.<br>
                    Der Rückgabewert ist die Länge des in den Buffer kopierten Pfades (ohne die Null am Ende).<br>
                    <br>
                    Ich habe die MSSETUP.DLL mal ins SYSTEM32 kopiert, und mit folgendem Source funktioniert's.<br>
                    (jedenfalls bei mir, D5 pro SP1)<br>
                    <br>
                    <pre>
                    unit MAIN;

                    interface

                    uses
                    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
                    StdCtrls;

                    type
                    TForm1 = class(TForm)
                    Button1: TButton;
                    procedure Button1Click(Sender: TObject);
                    private
                    { Private-Deklarationen }
                    public
                    { Public-Deklarationen }
                    end;

                    var
                    Form1: TForm1;

                    Function FindFileInTree(Const szFile : PChar;
                    Const szDir : PChar;
                    Const szBuf : PChar;
                    Const cbBuf : Integer
                    ): LongInt;
                    stdCall;external 'MSSETUP.DLL' name 'FindFileInTree';

                    implementation

                    {$R *.DFM}

                    procedure TForm1.Button1Click(Sender: TObject);

                    Var
                    sBuffer : String;
                    sFileName : String;
                    sRoot : String;
                    lCount : LongInt;

                    begin
                    sBuffer := StringOfChar(#00,MAX_PATH+1); // Speicherplatz reservieren
                    sFileName := 'MSSETUP.DLL'; // Nach dieser Datei suchen,
                    sRoot := 'C:\WINNT'; // und zwar ab hier
                    lCount := FindFileInTree( // Und los geht's
                    PChar(sFileName), // Gesuchte Datei
                    PChar(sRoot), // ab hier suchen
                    PChar(sBuffer), // hier wird das Ergebnis abgelegt
                    MAX_PATH); // Soviel Platz steht für das Ergebnis zur Verfügung
                    ShowMessage(Trim(sBuffer));
                    ShowMessage(IntToStr(lCount));
                    end;

                    end.
                    </pre>
                    <br>
                    Ciao<br>
                    Chri

                    Comment


                    • #11
                      Hi<br>

                      man kann diese vielen Unwägbarkeiten umgehen, indem man selber diese Funktion in Delphi nachbildet. Diese dürfte nicht langsammer sein als diese "API-Funktion" !

                      <html>
                      <head>
                      <title>Untitled</title>
                      </head>
                      <!-- Generated by ConTEXT HTML exporter -->
                      <body text="#000000" bgcolor="#D0D0D0">
                      <pre>
                      <code><font face="Courier New"><font color="#000000"><b>function </b></font>FindFileInTree<font color="#000000">(<b>const </b></font>Path<font color="#000000">, </font>FileName<font color="#000000">: <b>String</b>): <b>String</b>;

                      <b>function </b></font>FindRecursive<font color="#000000">(<b>const </b></font>Path<font color="#000000">, </font>FileName<font color="#000000">: <b>String</b>): <b>String</b>;
                      <b>var
                      </b></font>SR<font color="#000000">: </font>TSearchRec<font color="#000000">;
                      <b>begin
                      if </b></font>FindFirst<font color="#000000">(</font>Path <font color="#000000">+ </font>FileName<font color="#000000">, </font>faAnyFile<font color="#000000">, </font>SR<font color="#000000">) = </font><font color="#0000FF">0 </font><font color="#000000"><b>then
                      begin
                      </b></font>Result <font color="#000000">:= </font>Path <font color="#000000">+ </font>SR<font color="#000000">.</font>Name<font color="#000000">;
                      </font>FindClose<font color="#000000">(</font>SR<font color="#000000">);
                      <b>end else
                      begin
                      </b></font>Result <font color="#000000">:= </font><font color="#0000FF">''</font><font color="#000000">;
                      <b>if </b></font>FindFirst<font color="#000000">(</font>Path <font color="#000000">+ </font><font color="#0000FF">'*.*'</font><font color="#000000">, </font>faDirectory <font color="#000000"><b>or </b></font>faHidden <font color="#000000"><b>or </b></font>faSysFile<font color="#000000">, </font>SR<font color="#000000">) = </font><font color="#0000FF">0 </font><font color="#000000"><b>then
                      begin
                      repeat
                      if </b>(</font>SR<font color="#000000">.</font>Attr <font color="#000000"><b>and </b></font>faDirectory <font color="#000000">&lt;&gt; </font><font color="#0000FF">0</font><font color="#000000">) <b>and
                      </b>(</font>SR<font color="#000000">.</font>Name <font color="#000000">&lt;&gt; </font><font color="#0000FF">'.'</font><font color="#000000">) <b>and </b>(</font>SR<font color="#000000">.</font>Name <font color="#000000">&lt;&gt; </font><font color="#0000FF">'..'</font><font color="#000000">) <b>then
                      </b></font>Result <font color="#000000">:= </font>FindRecursive<font color="#000000">(</font>Path <font color="#000000">+ </font>SR<font color="#000000">.</font>Name <font color="#000000">+ </font><font color="#0000FF">'\'</font><font color="#000000">, </font>FileName<font color="#000000">);
                      <b>until </b>(</font>Result <font color="#000000">&lt;&gt; </font><font color="#0000FF">''</font><font color="#000000">) <b>or </b>(</font>FindNext<font color="#000000">(</font>SR<font color="#000000">) &lt;&gt; </font><font color="#0000FF">0</font><font color="#000000">);
                      </font>FindClose<font color="#000000">(</font>SR<font color="#000000">);
                      <b>end</b>;
                      <b>end</b>;
                      <b>end</b>;

                      <b>begin
                      </b></font>Result <font color="#000000">:= </font>FindRecursive<font color="#000000">(</font>Path <font color="#000000">+ </font><font color="#0000FF">'\'</font><font color="#000000">, </font>FileName<font color="#000000">);
                      <b>end</b>;

                      </font></font>
                      </code></pre>
                      </body>
                      </html>

                      Hage

                      Comment


                      • #12
                        um mit Wildcards mehrere Dateien zu finden geht auch das:<br>

                        <html>
                        <head>
                        <title>Untitled</title>
                        </head>
                        <!-- Generated by ConTEXT HTML exporter -->
                        <body text="#000000" bgcolor="#D0D0D0">
                        <pre>
                        <code><font face="Courier New"><font color="#000000"><b>procedure </b></font>FindFilesInTree<font color="#000000">(</font>Files<font color="#000000">: </font>TStrings<font color="#000000">; <b>const </b></font>Path<font color="#000000">, </font>FileName<font color="#000000">: <b>String</b>);

                        <b>procedure </b></font>FindRecursive<font color="#000000">(</font>Files<font color="#000000">: </font>TStrings<font color="#000000">; <b>const </b></font>Path<font color="#000000">, </font>FileName<font color="#000000">: <b>String</b>);
                        <b>var
                        </b></font>SR<font color="#000000">: </font>TSearchRec<font color="#000000">;
                        <b>begin
                        if </b></font>FindFirst<font color="#000000">(</font>Path <font color="#000000">+ </font>FileName<font color="#000000">, </font>faAnyFile<font color="#000000">, </font>SR<font color="#000000">) = </font><font color="#0000FF">0 </font><font color="#000000"><b>then
                        begin
                        repeat
                        if </b></font>SR<font color="#000000">.</font>Attr <font color="#000000"><b>and </b>(</font>faDirectory <font color="#000000"><b>or </b></font>faVolumeID<font color="#000000">) = </font><font color="#0000FF">0 </font><font color="#000000"><b>then
                        </b></font>Files<font color="#000000">.</font>Add<font color="#000000">(</font>Path <font color="#000000">+ </font>SR<font color="#000000">.</font>Name<font color="#000000">);
                        <b>until </b></font>FindNext<font color="#000000">(</font>SR<font color="#000000">) &lt;&gt; </font><font color="#0000FF">0</font><font color="#000000">;
                        </font>FindClose<font color="#000000">(</font>SR<font color="#000000">);
                        <b>end</b>;
                        <b>if </b></font>FindFirst<font color="#000000">(</font>Path <font color="#000000">+ </font><font color="#0000FF">'*.*'</font><font color="#000000">, </font>faDirectory <font color="#000000"><b>or </b></font>faHidden <font color="#000000"><b>or </b></font>faSysFile<font color="#000000">, </font>SR<font color="#000000">) = </font><font color="#0000FF">0 </font><font color="#000000"><b>then
                        begin
                        repeat
                        if </b>(</font>SR<font color="#000000">.</font>Attr <font color="#000000"><b>and </b></font>faDirectory <font color="#000000">&lt;&gt; </font><font color="#0000FF">0</font><font color="#000000">) <b>and
                        </b>(</font>SR<font color="#000000">.</font>Name <font color="#000000">&lt;&gt; </font><font color="#0000FF">'.'</font><font color="#000000">) <b>and </b>(</font>SR<font color="#000000">.</font>Name <font color="#000000">&lt;&gt; </font><font color="#0000FF">'..'</font><font color="#000000">) <b>then
                        </b></font>FindRecursive<font color="#000000">(</font>Files<font color="#000000">, </font>Path <font color="#000000">+ </font>SR<font color="#000000">.</font>Name <font color="#000000">+ </font><font color="#0000FF">'\'</font><font color="#000000">, </font>FileName<font color="#000000">);
                        <b>until </b></font>FindNext<font color="#000000">(</font>SR<font color="#000000">) &lt;&gt; </font><font color="#0000FF">0</font><font color="#000000">;
                        </font>FindClose<font color="#000000">(</font>SR<font color="#000000">);
                        <b>end</b>;
                        <b>end</b>;

                        <b>begin
                        if </b></font>Files <font color="#000000">&lt;&gt; <b>nil then
                        begin
                        </b></font>Files<font color="#000000">.</font>BeginUpdate<font color="#000000">;
                        <b>try
                        </b></font>FindRecursive<font color="#000000">(</font>Files<font color="#000000">, </font>Path <font color="#000000">+ </font><font color="#0000FF">'\'</font><font color="#000000">, </font>FileName<font color="#000000">);
                        <b>finally
                        </b></font>Files<font color="#000000">.</font>EndUpdate<font color="#000000">;
                        <b>end</b>;
                        <b>end</b>;
                        <b>end</b>;

                        </font></font>
                        </code></pre>
                        </body>
                        </html&gt

                        Comment

                        Working...
                        X