Solved

This is easy

Posted on 1998-03-30
5
233 Views
Last Modified: 2010-04-06
Is there any built in functions that can return me the number of files found (Including sub directories) when i pass in a directory path. I just want the number of files found not including the directories

If there isn't can anyone show me how to do this??

Thanks in Advance,
Ermac
0
Comment
Question by:ermac
5 Comments
 

Expert Comment

by:Dippen
Comment Utility
Use the FindFirst and the FindNext functions for that problem. This function would do the job (written in D3):

function TForm1.CountFiles(Path : shortstring) : Integer;
var
  SearchResult : TSearchRec;
  ErrorCode,
  LocalResult : Integer;

begin
  Result := 0;
  LocalResult := 0;
  ErrorCode := FindFirst(path+'\*.*',faDirectory,SearchResult);
  while (ErrorCode = 0) do begin
    Inc(LocalResult);
    ErrorCode := FindNext(SearchResult);
  end;
  result := LocalResult;
end;
 

//Daniel, Sweden
0
 

Author Comment

by:ermac
Comment Utility
Hi Dippen

You solution doesn't check for sub directories...
I need to count the number of files in the sub directories as well

Probably have to use some recursion...

Ermac
0
 
LVL 5

Expert Comment

by:julio011597
Comment Utility
Then you need a CountFiles(), as written by Dippen, incapsulated in a tree traversal algorithm - do you need some code, or a plain-english algorithm description?
0
 
LVL 1

Expert Comment

by:lowlevel
Comment Utility
you'll have to modify this a bit, but I think this should do it.

var
  count          : record
    filesize       : cardinal;
    realsize       : cardinal;
    num            : cardinal;
    attrOccur      : array[tAttr] of cardinal;
  end;

procedure getAFileSpec (startpath,filespec : string);
var
  sr        : tSearchRec;
  finished  : integer;
  goSub     : boolean;
begin
//first get all files/dirs matching the file-spec and process them.
//if the user wants fDoFSSubs, that's done here too...
  finished:=findFirst (startPath+filespec, faAnyFile, sr);
  while finished=0 do begin
    doThing (startpath,sr);
    if (fDoFSSubs in todolist) and      //do we even want this?
       (sr.attr and faDirectory >0) and  //err, is this a directory?
       (sr.name[1]<>'.')                 //not the . and .. dir's, please
      then getAFileSpec (startPath+sr.name+'\', fileSpec);
    finished:=findNext (sr);
  end;
  if fDoDebug in todolist then writeln ('Err: ',finished);
  findClose (sr);

//need to do process ALL sub-dirs?
  if (fDoAllSubs in toDoList) {and (filespec<>'*.*')} then begin
    finished:=findFirst (startPath+'*.*', faDirectory, sr);
    while finished=0 do begin
      if (sr.name<>'.') and (sr.name<>'..')
        then getAFileSpec (startPath+sr.name+'\', filespec);
      finished:=findNext (sr);
    end;
    if fDoDebug in todolist then writeln ('Err2: ',finished);
    findClose (sr);
  end;
end;

procedure doThing (startpath : string; sr : tSearchRec);
  procedure doCount;
  var
    size: cardinal;
    i    : tAttr;
  begin
    if sr.Size<HDDClustSize
      then size:=HDDClustSize
      else size:=sr.size;
    inc (count.filesize, sr.size);
    inc (count.realsize, size);
    inc (count.num);
  //keep track of what attributes occur how often
    for i:=low (tAttr) to high(tAttr) do begin
      if ((sr.attr and ord(attrVal[i])) > 0) then inc (count.attrOccur[i])
    end;
  end;

  procedure doDelSubStr;
  var
    strToDel : string;
    newName  : string;
    foundAt,
    l,
    i        : integer;
  begin
  //get the full string to delete first.
    for i:=1 to 9 do
      if paramstr(i)=stuffItCanDo[fDelSubStr].str then break;
    strToDel:=paramstR(i+1);

    if (strToDel[1]='-') then begin
      writeln ('ERROR [during deleting] : parameters are wrong!');
      exit;
    end;

  //modify name
    newName:=sr.name;
    foundAt:=instring (newName,strToDel,false, 0);
    while foundAt<>0 do begin
      delete (newName, foundAt, length (strToDel));
      foundAt:=instring (newName,strToDel,false, 0);
    end;
    if sr.name<>newname then
      if not renameFile (startPath+sr.name, startPath+newname) then debug ('Err: could not rename file');
  end;

  procedure doRepSubStr;
  var
    strToDel : string;
    strToRep : string;
    newName  : string;
    foundAt,
    l,
    i        : integer;
  begin
  //get the parameters
    for i:=1 to 9 do
      if paramstr(i)=stuffItCanDo[fRepSubStr].str then break;
    strToDel:=paramstR(i+1);
    strToRep:=paramstr(i+2);

    if (length(strToDel)<1) or (length(strToRep)<1) then begin
      writeln ('ERROR [during replacing] : parameters are wrong!');
      exit;
    end else
      if (strToDel[1]='-') or (strToRep[1]='-') then begin
        writeln ('ERROR [during replacing] : parameters are wrong!');
        exit;
      end;

  //modify name
    newName:=sr.name;
    foundAt:=instring (newName,strToDel,false, 0);
    while foundAt<>0 do begin
      delete (newName, foundAt, length (strToDel));
      insert (strToRep, newName, foundAt);
      foundAt:=instring (newName,strToDel,false, 0);
    end;
    if sr.name<>newname then
      if not renameFile (startPath+sr.name, startPath+newname) then debug ('Err: could not rename file');
  end;

begin
  if (sr.name='.') or (sr.name='..') then exit;
//counting
  if fCount in todolist then doCount;
//deleting sub-strings from filenames
  if fDelSubStr in todolist then doDelSubStr;
  if fRepSubStr in todolist then doRepSubStr;

//test
  if fDoDebug in todolist then
    if ((sr.attr and faDirectory)>0)
      then writeln ('dir : '+startPath+sr.name)
      else writeln ('file: '+startPath+sr.name);
end;

0
 
LVL 3

Accepted Solution

by:
williams2 earned 20 total points
Comment Utility
ok, this one does the work!

function TForm1.CountFiles(Path : string) : Integer;
var
  SearchResult : TSearchRec;
  ErrorCode,
  LocalResult : Integer;
begin
  Result:= 0;
  LocalResult := 0;
  ErrorCode := FindFirst(path+'\*.*',faAnyFile,SearchResult);
  while (ErrorCode = 0) do
  begin
    If (SearchResult.Name<>'.') AND (SearchResult.Name<>'..') then
    Begin
      If (SearchResult.Attr AND faDirectory)>0 then
        Inc(LocalResult,CountFiles(Path+'\'+SearchResult.Name))
      else
        Inc(LocalResult);
    End;
    ErrorCode := FindNext(SearchResult);
  end;
  result := LocalResult;
end;

Regards
Williams
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now