Count folder contents?

Hi,
I have the code below, which successfully adds folders to my listbox.
I am struggling with trying to display the number of items contained in each folder, in column 1 (the second column).

I know I need to use f.count somewhere, but I’m confused how to go about it :frowning:

[code] Dim itemsAdded As Integer
Dim f As FolderItem
Dim fileCount As String

Dim vol As New FolderItem
f = vol.GetRelative(RowTag(row))

For i As Integer = 1 To f.Count
If f.Item(i).Visible Then
If f.Item(i).Directory = True Then
Me.AddFolder(f.Item(i).Name)
Else
Me.Addrow(f.Item(i).Name)
End If

  itemsAdded = itemsAdded+1
  
  // INSERT PATH INTO A NON-VISIBLE COLUMN
  RowTag(row + itemsAdded) = f.Item(i).GetSaveInfo(GetFolderItem(""))
  
  
  ////////////////////// The code segment below displays a minus sign in the second column (column 1) of the listbox
  
  If f.Item(i).Directory Then
    fileCount = "-" // WHAT GOES HERE IN ORDER TO COUNT THE NUMBER OF ITEMS INSIDE, AS OPPOSED TO JUST DISPLAYING A MINUS SIGN ??
  End If
  
  Cell(row+itemsAdded, 1) = fileCount
  
  
  //////////////////////////
  
  
  
End If

Next[/code]

      If f.Item(i).Directory = True Then
        Me.AddFolder Array(f.Item(i).Name, Str(f.Item(i).Count))
      Else
        Me.Addrow(f.Item(i).Name)
      End If

Asis - this throws an error:

Parameter “Text” expects type string, but this is type string()

I can’t see the image. What’s the error?

Parameter “Text” expects type string, but this is type string()

My code now looks like this:

[code]Dim itemsAdded As Integer
Dim f As FolderItem
Dim fileCount As String

Dim vol As New FolderItem
f = vol.GetRelative(RowTag(row))

For i As Integer = 1 To f.Count
If f.Item(i).Visible Then
If f.Item(i).Directory = True Then
Me.AddFolder(f.Item(i).Name)
Else
Me.Addrow(f.Item(i).Name)
End If

  itemsAdded = itemsAdded+1
  
  // INSERT PATH INTO A NON-VISIBLE COLUMN
  RowTag(row + itemsAdded) = f.Item(i).GetSaveInfo(GetFolderItem(""))
  
  
  ////////////////////// The code segment below displays a minus sign in the second column (column 1) of the listbox
  
  //If f.Item(i).Directory Then
  //fileCount = "-"
  //End If
  
  If f.Item(i).Directory = True Then
    Me.AddFolder Array(f.Item(i).Name, Str(f.Item(i).Count))
  Else
    Me.Addrow(f.Item(i).Name)
  End If
  
  Cell(row+itemsAdded, 1) = fileCount
  
  
  //////////////////////////
  
  
  
End If

Next[/code]

You’re code is not efficient enough. You should avoid coding like that. It’ll produce few unnecessary bugs and hard to understand when read the code again in the next few months.

Remove my code above and use this instead,

  If f.Item(i).Directory Then
    //fileCount = "-" // WHAT GOES HERE IN ORDER TO COUNT THE NUMBER OF ITEMS INSIDE, AS OPPOSED TO JUST DISPLAYING A MINUS SIGN ??
    fileCount = Str(f.item(i).Count)
  End If
  
  Cell(row+itemsAdded, 1) = fileCount

Thanks Asis - I will try that code.
The code I am using was extracted from a project file in the Example Projects directory :slight_smile:

For maximum speed, you can check the DirectorySizeMBS class here:
http://www.monkeybreadsoftware.net/class-directorysizembs.shtml

Kinda spammy, nobody asked for alternate options; Richard wanted help with where to use f.count.

Christian’s class is indeed MUCH faster than native. I would not call that spammy. Maybe pluggy, but it is within topic.

But that’s not what the post was about, and that’s when a plug becomes spam.

I appreciate MBS for the functionality extensions it offers, but posting about it all the damn time is not helpful.

Have you taken your pills this morning ? Sorry :wink: Seriously, I don’t see much harm in Christian’s plugs. I feel this particular one is within topic so no harm done.