KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Recurse and Count Files in Folders
PRODUCT: 4D | VERSION: 2003 | PLATFORM: Mac & Win
Published On: May 27, 2004
Your current browser may not allow you to download the asset. We recommend Mozilla Firefox or Google Chrome.
Log In

How can I count all of the files in a folder and files in its subfolders and so on?

Counting files only requires a simple method that will traverse all subfolders and count files accordingly. This method starts at the base folder. It counts the files in that folder and starts with its subfolders (if any). By calling itself, it can perform the same action of counting files as it traverses through the directory tree.

C_STRING(255;$1)
ARRAY TEXT($FolderList;0)
ARRAY TEXT($DocumentList;0)
C_LONGINT($platform;$i)
C_LONGINT($0;$count)

 ` count folders and documents
FOLDER LIST($1;$FolderList)
DOCUMENT LIST($1;$DocumentList)
$count:=Size of array($DocumentList)

If (Size of array($FolderList)>0)
  For ($i;1;Size of array($FolderList))
    PLATFORM PROPERTIES($platform)

  ` count documents
    If ($platform#3)
      $count:=$count+Helper_CountFiles ($1+$FolderList{$i}+":")
    Else
      $count:=$count+Helper_CountFiles ($1+$FolderList{$i}+"\\")
    End if

  End for
End if

$0:=$count

To test, have another method that will pass a path:
$count:=Helper_CountFiles(Select folder("Select a folder to count files"))