Board index » delphi » Recursively searching Directories for files! (HELP)

Recursively searching Directories for files! (HELP)

Quote
Robert Gilland wrote:
> I need to search the whole computer or even the whole network
> using delphi I know I can do it from the win95 desktop but I
> need to be able to do it programmatically. can anyone help?

 The FindFirst/FindNext functions should do it nicely.

--
Martin Brooks - Systems Administrator
=====================================
Image Data Systems (UK) Ltd
82-84 Clerkenwell Road
London
EC1M 5RJ

  T: 0171 336 7942
  F: 0171 336 7943
  E: martin(at)image-data(dot)com
UIN: 2178117

* abuse@localhost postmaster@localhost root@localhost *

- Computers make very fast, very accurate mistaks -

 

Re:Recursively searching Directories for files! (HELP)


I need to search the whole computer or even the whole network
using delphi I know I can do it from the win95 desktop but I
need to be able to do it programmatically. can anyone help?

Re:Recursively searching Directories for files! (HELP)


Quote
Robert Gilland wrote:
> I need to search the whole computer or even the whole network
> using delphi I know I can do it from the win95 desktop but I
> need to be able to do it programmatically. can anyone help?

Hi Robert

The following function recurses down sub directories calculating the
disk space consumed. It uses FindFirst - FindNext - and FindClose
to walk through the directories. I am not sure how you could get
the entire network searched though.

DirName should be a path (without a trailing \)

Hope this helps

John

function recursive_size(DirName : String ; BlockSize : Integer ):
LongInt;
var
 Accum : Longint;
 SearchRec : TSearchRec;
begin
 Accum := 0;
 {look for files}
 if (FindFirst(DirName +'\*.*',$2F,SearchRec) = 0) then
 begin
  repeat
  begin
   if ((SearchRec.Attr and faDirectory) = 0) then
   begin
    If (BlockSize > 1) then
    begin
     {round up to nearest block size}
     Inc(Accum,(((SearchRec.Size-1) div BlockSize) + 1) * BlockSize);
    end
    else
    begin
     Inc(Accum,SearchRec.Size);
    end;
   end;
  end
  Until (FindNext(SearchRec) <> 0);
  SysUtils.FindClose(SearchRec);
 end;

 {look for Directories}
 if (FindFirst(DirName +'\*.*',$10,SearchRec) = 0) then
 begin
  repeat
  begin
   if ((SearchRec.Attr and faDirectory) > 0) and
    (SearchRec.Name <> '.' ) and (SearchRec.Name <> '..') then
    Inc(Accum,Recursive_Size(DirName + '\' + SearchRec.Name,
BlockSize));
  end
  Until (FindNext(SearchRec) <> 0);
  SysUtils.FindClose(SearchRec);
 end;
 result := Accum;
end;

Other Threads