Board index » delphi » DOS Unit and searching an entire HDD

DOS Unit and searching an entire HDD

Hallo all

1. Is there code that will let me obtain the entire directory tree of a
specific drive.
Using FSearch/Chdir/ etc. etc. one can search a specific drive or directory
and get information regarding those files/directories using SearchRec etc.

2. Would it be possible then to search an entire HDD for a file say
"123.xls" without having to know beforehand exactly which directory it is in
.

Any / all help will be greatly appreciated

Thanks for a wonderful newsgroup
Lawrence Breakey

 

Re:DOS Unit and searching an entire HDD


Quote
Lawrence Breakey (lbrea...@ilink.nis.za) wrote:

: 1. Is there code that will let me obtain the entire directory tree of a
: specific drive.
: Using FSearch/Chdir/ etc. etc. one can search a specific drive or directory
: and get information regarding those files/directories using SearchRec etc.
:
: 2. Would it be possible then to search an entire HDD for a file say
: "123.xls" without having to know beforehand exactly which directory.

       Here is a quick program to see two directories
deep (root and sub-directories), maybe it will give
you a start to write what you want.   This program
worked pretty good when I wrote it, DOS boxes were
not very big, but displaying all directories now on
a Win95 machine is a challenge.
       Maybe somebody else can answer the other questions. :-)

Program See_2_DeepColorTree;   { by Joe Fischer }
uses Dos, Crt;    { shows only root and sub-directories of root }
type  menu = array[1..200] of String[32];
var     sub,root            : menu;
        Dirinfo            :SearchRec;
        Indx,oldIndx,Nomo,ckey,sc,
        CurrDirectory,CurrPath,s      :string[80];
        Up,RootSize,colr,col,row,loopr,lf    :integer;
        CurrentPath   : String[80];

Procedure Read_Root;
begin
   FindFirst(CurrentPath,AnyFile,Dirinfo);
   While DosError = 0 do     begin
         If DirInfo.Attr = $10 then  begin
             Root[Up]:= Dirinfo.Name; Inc(Up); end;
         FindNext(DirInfo);    end;  RootSize:= Up;
end;

Procedure Read_Sub;
begin
   FindFirst(CurrentPath,AnyFile,Dirinfo);
   While DosError = 0 do
      begin
         If DirInfo.Attr = $10 then
            begin
               sub[Up]:= Dirinfo.Name;
               if sub[Up] <> '.'
                  then if sub[Up] <> '..'
                     then Inc(Up);
            end;  (* DirInfo.Attr *)
         FindNext(DirInfo);
      end;
end;

Procedure Start;
begin
   For loopr:= 1 to 200 do
      begin
         root[loopr]:= '';
         sub[loopr]:= '';
      end;
   TextBackground(0); ClrScr; Up:= 1; colr:= 9;
end;

Procedure ReadLevelOne;
var     sp,loop,column    :integer;
begin
   CurrentPath:= 'C:\' + Root[lf] + '\*.*'; Read_Sub;
   TextBackGround(colr); TextColor(0);
   column:= WhereX; Write(Root[lf]);
   for sp:= 1 to 14 - ord(root[lf][0]) do  write(' ');
   TextBackGround(0);
   if colr = 14 then TextColor(colr - 8)
      ELSE TextColor(colr);
   For loop:= 1 to Up - 1 do  begin
      if loop = 1 then begin GotoXY(column,WhereY + 1);
                             writeln('----- ',Sub[loop]); end
         ELSE begin GotoXY(column,WhereY);
              Writeln('----- ',Sub[loop]); end;
                             end;
   colr:= colr + 1; if colr > 15 then colr:= 1;
   if colr = 8 then colr:= 9; Up:= 1; if whereX > 50 then writeln;
end;

begin        (*************  M A I N  ******************)
   Start; CurrentPath:= 'C:\*.*'; Read_Root; Up:= 1;
   GotoXY(10,25); Write('2 DEEP    by   Joe Fischer');
   GotoXY(1,1); For lf:= 1 to RootSize do  ReadLevelOne;
   Readln; TextBackGround(0); TextColor(7); TextMode (C80); ClrScr;
end.

Re:DOS Unit and searching an entire HDD


JRS:  In article <357c3da...@mnn.mweb.co.za> of Mon, 8 Jun 1998 21:34:10
in comp.lang.pascal.borland, Lawrence Breakey <lbrea...@ilink.nis.za>
wrote:

Quote
>1. Is there code that will let me obtain the entire directory tree of a
>specific drive.
>Using FSearch/Chdir/ etc. etc. one can search a specific drive or directory
>and get information regarding those files/directories using SearchRec etc.
>2. Would it be possible then to search an entire HDD for a file say
>"123.xls" without having to know beforehand exactly which directory it is in

1.   Yes.       2.   Yes.

In http://www.merlyn.demon.co.uk/programs/ (q.v. Sig), see listdirs.pas
and hunt.pas

--
John Stockton, Surrey, UK.    j...@merlyn.demon.co.uk    Turnpike v1.12    MIME.
  Web <URL:http://www.merlyn.demon.co.uk/> -- FAQqish topics, acronyms & links.
  PAS & EXE in <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.txt.
  Do not Mail News to me.    Before a reply, quote with ">" or "> " (SoRFC1036)

Re:DOS Unit and searching an entire HDD


Yes, that is possible. It is called recursion. Recursion is a special
programming trick by which you can let a procedure call itself. Simply
speaking you write a routine that reads a directory until a directory is
found and jumps to that directory. Then you call the same procedure within
that procedure. Now the new dir is read until a dir comes along and the same
procedure is called, etc. At the end of a routine it ends and gives the
control back to the procedure that called it in the first place.

You have to be very carefull with recursion. You HAVE to make sure that the
recursed routine ends somewhere, because otherwise you will get an endless
loop. In other words: a lock-up!

<357c3da...@mnn.mweb.co.za>...

Quote
>Hallo all

>1. Is there code that will let me obtain the entire directory tree of a
>specific drive.
>Using FSearch/Chdir/ etc. etc. one can search a specific drive or directory
>and get information regarding those files/directories using SearchRec etc.

>2. Would it be possible then to search an entire HDD for a file say
>"123.xls" without having to know beforehand exactly which directory it is
in
>.

>Any / all help will be greatly appreciated

>Thanks for a wonderful newsgroup
>Lawrence Breakey

Re:DOS Unit and searching an entire HDD


Quote
Lawrence Breakey wrote in message <357c3da...@mnn.mweb.co.za>...
>2. Would it be possible then to search an entire HDD for a file say
>"123.xls" without having to know beforehand exactly which directory it is

in

  Yes.

--------------------------------------------------------------------
 UNIT Search;
INTERFACE
  Uses Dos;

   Function Find(StartDir,FileName :string): String;

IMPLEMENTATION

Procedure LCase(Var S: String);
  Var
    Nmb   :Word;
  Begin
   For Nmb:=1 to Ord(S[0])
    Do S[Nmb]:=UpCase(S[Nmb]);
  End;

Function Find(StartDir,FileName :string): String;
  Var
    FileInfo      : searchRec;
    f             : File;
    path2         : pathStr;
    s             : String;
  Begin
   LCase(StartDir);
   LCase(FileName);
   Find:='';
   FindFirst (StartDir+'\*.*', AnyFile, FileInfo);
     While DosError = 0 Do
      begin
        if (FileInfo.Name[1] <> '.')and(FileInfo.attr <> VolumeId)
          then if ((FileInfo.Attr and Directory) = Directory)
           then Begin
                     Path2 := StartDir+'\'+FileInfo.Name;
                     S:=Find(path2, FileName);
                     If S<>'' Then Begin {File was found...}
                                            Find:=S;
                                            Exit;
                                           End;
                     End
           else if ((FileInfo.Attr and VolumeID) <> VolumeID)
           Then Begin
                      S:=FileInfo.Name;
                       LCase(S);
                       If S=FileName
                        Then Begin
                                   Find:=StartDir+'\'+FileName;
                                   Exit;
                                 End;
                    End;
        FindNext(FileInfo);
      End;
End;

Begin {Unit initialization...}
End.

  All the best...

Other Threads