Board index » delphi » Searching subdirectories

Searching subdirectories

How can I search for a file in a specific directory and his sub directories?

Please e-mail me at W.San...@inter.NL.net

Thank you.

 

Re:Searching subdirectories


Quote
van der Sanden wrote in message <73hmca$ej...@newnews.nl.uu.net>...
>How can I search for a file in a specific directory and his sub
directories?

>Please e-mail me at W.San...@inter.NL.net

>Thank you.

Use the recursive approach:

1.  search files in the given directory
2.  note any subdirectories found
3.  if file found, then stop, if not...
4.  repeat from step 1 with every subdirectory found.

Hope this helps,

Nikita.

Re:Searching subdirectories


In article <73hmca$ej...@newnews.nl.uu.net>,
van der Sanden <W.San...@inter.NL.net> wrote:
:How can I search for a file in a specific directory and his sub directories?

 151578 Oct 10 1998 ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip
 tsfaqp.zip Common Turbo Pascal Questions and Timo's answers, linked

12. *****
 Q: How to find the files in a directory AND subdirectories?

 A: Writing a program that goes through the files of the directory,
and all the subdirectories below it, is based on Turbo Pascal's file
finding commands and recursion. This is universal whether you are
writing, for example, a directory listing program, or a program that
deletes, say, all the .bak files, or some other similar task.
   To find (for listing or other purposes) the files in a directory
you need above all the FindFirst and FindNext keywords, and testing
the predefined file attributes. You make these a procedure, and call
it recursively. If you want good examples with source code, please
see PC World, April 1989, p. 154; Kent Porter & Mike Floyd (1990),
Stretching Turbo Pascal. Version 5.5. Revised Edition, Chapter 23;
Michael Yester (1989), Using Turbo Pascal, p. 437; Michael Tischer
(1992), PC Intern System Programming, pp. 796-798.
   The simple (non-recursive) example listing all the read-only
files in the current directory shows the rudiments of the principle
of Using FindFirst, FindNext, and the file attributes, because some
users find it hard to use these keywords. Also see the code in the
item "How to establish if a name refers to a directory or not?" of
this same FAQ collection you are now reading.
    uses Dos;
    var FileInfo : SearchRec;
    begin
      FindFirst ('*.*', AnyFile, FileInfo);
      while DosError = 0 do
        begin
          if (FileInfo.Attr and ReadOnly) > 0 then
            writeln (FileInfo.Name);
          FindNext (FileInfo);
        end;
    end.  (* test *)

 A2: While we are on the subject related to FindFirst and FindNext,
here are two useful examples:

(* Number of files in a directory (not counting directories) *)
function DFILESFN (dirName : string) : word;
var nberOfFiles  : word;
    FileInfo     : searchRec;
begin
  if dirName[Length(dirName)] <> '\' then dirName := dirName + '\';
  dirName := dirName + '*.*';
  {}
  nberOfFiles := 0;
  FindFirst (dirName, AnyFile, FileInfo);
  while DosError = 0 do
    begin
      if ((FileInfo.Attr and VolumeId) = 0) then
        if (FileInfo.Attr and Directory) = 0 then
          Inc (nberOfFiles);
      FindNext (FileInfo);
    end; {while}
  dfilesfn := nberOfFiles;
end;  (* dfilesfn *)

(* Number of immediate subdirectories in a directory *)
function DDIRSFN (dirName : string) : word;
var nberOfDirs : word;
    FileInfo    : searchRec;
begin
  if dirName[Length(dirName)] <> '\' then dirName := dirName + '\';
  dirName := dirName + '*.*';
  {}
  nberOfDirs:= 0;
  FindFirst (dirName, AnyFile, FileInfo);
  while DosError = 0 do
    begin
      if ((FileInfo.Attr and VolumeId) = 0) then
        if (FileInfo.Attr and Directory) > 0 then
          if (FileInfo.Name <> '.') and (FileInfo.Name <> '..') then
            Inc (nberOfDirs);
      FindNext (FileInfo);
    end; {while}
  ddirsfn := nberOfDirs;
end;  (* ddirsfn *)
--------------------------------------------------------------------

108. *****
 Q: How can I list with paths all the files on a drive?

 A: Here is the example source code
  {$M 16384,0,0}
  Uses Dos;
  {... the top directory ...}
  procedure FindFiles (Path, FileSpec : string);
  var FileInfo : SearchRec;
  begin
    FindFirst (Path + FileSpec, AnyFile, FileInfo);
    while DosError = 0 do begin
      if ((FileInfo.Attr and Directory) = 0) and
         ((FileInfo.Attr and VolumeId) = 0) then begin
        writeln (Path+FileInfo.Name);
      end; {if}
      FindNext (FileInfo);
    end; {while}
    {}
    {... subdirectories ...}
    FindFirst (Path + '*.*', Directory, FileInfo);
    while DosError = 0 do
      begin
        if ((FileInfo.Attr and Directory) > 0) and
            (FileInfo.Name <> '.') and
            (FileInfo.Name <> '..') then
              FindFiles (Path + FileInfo.Name + '\', FileSpec);
        FindNext (FileInfo);
      end; {while}
  end;  (* findfiles *)
  {}
  begin
    FindFiles ('C:\', '*.*');  { Note the trailing \ }
  end.
For starting below the root, use e.g. FindFiles ('C:\DOS\', '*.*');
--------------------------------------------------------------------

:Please e-mail me at W.San...@inter.NL.net

Why?

   All the best, Timo

....................................................................
Prof. Timo Salmi   Co-moderator of news:comp.archives.msdos.announce
Moderating at ftp:// & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance  ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/>  ; FIN-65101,  Finland

Spam foiling in effect.  My email filter autoresponder will return a
required email password to users not yet in the privileges database.
Advice on spam foiling at http://www.uwasa.fi/~ts/info/spamfoil.html

Re:Searching subdirectories


van der Sanden schrieb:
Quote

> How can I search for a file in a specific directory and his sub directories?

DOS or... ?
http://www.geocities.com/SiliconValley/2926/tpf.html
page FAQ, "Recursion example, program SUCHE"

Regards, Franz Glaser

Other Threads