How to compute the number of folders in a folder ?

Hi,

In a multimedia application, the user choose or drop a folder (directory) into the application to display its contents.

For some reasons, (two), I have to know the number of chiold directories in that master folder (directory).

How can I get that value ?
Scan the master directory, using Item(x), and inc a variable when the current item(x) is a directory ?

Nota: the master directory can have as much as 1999 items (maybe more in the future)

The code I have in mind is:

For LoopIdx = 1 to MDCnt // Master Directory Count
if MD.TrueItem(LoopIdx).Directory Then
DirCnt = DirCnt + 1
end if

If UserCancelled then exit
Next

Better use TrueItem, not just item. Use recursion to get subfolders.

if you need speed, consider using a special class like DirectorySizeMBS from our plugins.

This is the recursive code I use, found it somewhere on the Real Studio forum:

Sub ListItems(theItem as FolderItem) dim mItemCount, thisItem as Integer dim currentItem as FolderItem mItemCount = theItem.count //counts the item in the folder for thisItem = 1 to mItemCount currentItem = theItem.Item(thisItem) // get the next item in the folder if currentitem <> nil AND currentItem.exists = true then //NilObjectException Check if currentItem.Directory = true then // do something with the folder ListItems(currentItem) // recursive call else // do something with the file end if end if next End Sub

mmm copy/paste screws up th indentation… sorry for that. This works good for me. You should put your folder count var behind the
if currentitem.Directory = true statement…

Please use TrueItem to avoid endless recursion.

Thank you for your answers.

Alexander:
you do not need to add = true in your if test
if currentItem.Directory = true then
if currentitem <> nil AND currentItem.exists = true then

Also, since the reference comes from the master folder I (but I may be wrong) do not see why checking against Nil or Exists.

At last, in the mean time, I wrote the piece of code: no wait to display the window with a 813 items folder (804 folders found). Very fast !

Christian:
I used TrueItem in my pseudo (and final) code.

Use recursion to get subfolders
Why ? Can you explain ?

Here is my code:
Dim BookFI As FolderItem
Dim BookItems As Integer
Dim BookCnt As Integer
Dim LoopIdx As Integer

// 1. Search the number of valid folders in the current Book
// a. Get a local variable of the current Book FolderItem
BookFI = mFileIO.gBookFI

// b. Get the number of items in the current Book
BookItems = mFileIO.gBookFI.Count

// c. Scan the whole current Book contents and count the # of valid directories
For LoopIdx = 1 To BookItems // Number of items in the current Book
// Is this a directory ? (a directory with items ?)
If BookFI.TrueItem(LoopIdx).Directory And BookFI.TrueItem(LoopIdx).Count > 0 Then
// Yes, so inc the number of directories
BookCnt = BookCnt + 1
End If

// To avoid 1, Infinite Loop
If UserCancelled Then Exit

Next

Alexander:

I created a Method with your code and now I see why the Nil / Exists tests !
I do not talked about that because I already dealed with that before calling that code. Of course, I keep that information for me ;-:slight_smile:

It is incredible how colors look good / how my understanding of the code increase when it is displayed in colours!

Hello Emile,

I wrote for you a method using a shell ( if you run Mac) .
You sent a folderitem to the method and it returns the number of folders. I wrote the recursive and non recursive cases.

Counter_Folders(f_Source as folderitem) --> string

Dim s1 As Shell Dim Unix_Path as String //------------------------------------------------------------- if f_Source<>nil then //------------------------------------------------- // display the path - not essential //------------------------------------------------- TextField_Source.Text=f_Source.ShellPath //------------------------------------------------- // cleaning the path //------------------------------------------------- Unix_Path=ReplaceAll(f_Source.ShellPath,"\","") //------------------------------------------------- // unix //------------------------------------------------- s1= new Shell // non recursive --> it returns the number of folders s1.Execute "ls -Al "+""""+Unix_Path+""""+"| grep ^d | wc -l" // recursive --> it returns the number of folders //s1.Execute "ls -AlR "+""""+Unix_Path+""""+"| grep ^d | wc -l" // non recursive --> it returns the folders //s1.Execute "ls -Al "+""""+Unix_Path+""""+"| grep ^d" // recursive --> it returns the folders //s1.Execute "ls -AlR "+""""+Unix_Path+""""+"| grep ^d" //------------------------------------------------- // it returns the number of folders //------------------------------------------------- return s1.result '--- end if '--- //-------------------------------------------------------------

no frills

Counter_Folders(f_Source as folderitem) --> string

Dim s1 As Shell Dim Unix_Path as String //------------------- if f_Source<>nil then //------------------------------------------------- Unix_Path=ReplaceAll(f_Source.ShellPath,"\","") // cleaning the path s1= new Shell // non recursive --> it returns the number of folders s1.Execute "ls -Al "+""""+Unix_Path+""""+"| grep ^d | wc -l" // non recursive --> it returns the number of folders //s1.Execute "ls -AlR "+""""+Unix_Path+""""+"| grep ^d | wc -l" // recursive --> it returns the number of folders //s1.Execute "ls -Al "+""""+Unix_Path+""""+"| grep ^d" // non recursive --> it returns the folders //s1.Execute "ls -AlR "+""""+Unix_Path+""""+"| grep ^d" // recursive --> it returns the folders return s1.result // it returns the number of folders //------------------------------------------------- end if '---

Merci.

x-Plat?

For OS X, but I think the autor does not have a Windows machine to tet it.

Try it and let us know if it is X-Plat.

Hi all,

I use it on Mac. It’s a Unix command line , so it works on MacOS X and perhaps linux !
Command line is without comparison with any other method : I list all my mp3 ( ? 100 Go !) in a text file and open the text file in two seconds ! (I can share the code). And it’s a very short code.

No frills at all : Dim s1 As Shell Dim Unix_Path as String if f_Source<>nil then //------------------------------------------------- Unix_Path=ReplaceAll(f_Source.ShellPath,"\","") // cleaning the path s1= new Shell s1.Execute "ls -AlR "+""""+Unix_Path+""""+"| grep ^d | wc -l" // R : recursive return s1.result // it returns the number of folders //------------------------------------------------- end if '---

I know this is resurrectuing a pretty old thread, but still.

I saw this thread while I was looking for a declare-y way to find items recursively in a directory (especially with wildcards). I currently use “find” from a shell though, and thought to ask if anyone has tested how fast “find -type d” behaves compared to ls + grep

I haven’t found a way to use declares to get recursive directory contents. I assume the OS has the fastest knowledge of filesystem contents but I could be wrong.