Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

This is easy

Posted on 1998-03-30
5
Medium Priority
?
259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Expert Comment

by:Dippen
ID: 1361049
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
ID: 1361050
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
ID: 1361051
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
ID: 1361052
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 80 total points
ID: 1361053
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

636 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