user3793009
user3793009

Reputation:

All SubDirectories and a file count for each in a lable

How can i have the list of all SubDirectories and for each SubDirectory a file count?

And most important it must be in a form of continuos text, not as a listbox.

My example just prints the last Directory. How can i get it to print them all like this:

Adobe: 45 / Adobe Media Player:5 / Java: 22 / etc....

  Private Sub GetDir()
    For Each x As String In System.IO.Directory.GetDirectories("C:\ProgramFiles")
        y = x & " : " & CStr(x.Count)
    Next

    Label1.Text = y
  End Sub

I need it in continuos text so i can mail it actually. That's why i can't work with listbox.

Upvotes: 0

Views: 67

Answers (2)

user3453226
user3453226

Reputation:

You can use a StringBuilder object to build the output and a DirectoryInfo object to easily get name and files count for each directory.

Dim list As New StringBuilder
For Each directory As String In IO.Directory.GetDirectories("C:\ProgramFiles")
    Dim subDirectory As New IO.DirectoryInfo(directory)
    list.Append(subDirectory.Name & ": " & subDirectory.GetFiles.Length & " / ")
Next
Dim text As String = list.ToString.Remove(list.Length - 3)

Upvotes: 1

Adam
Adam

Reputation: 490

I had to add a space to "Program Files" or it was blowing up, but the += should add to your string in the loop fine.

Private Sub GetDir()

    Dim y As String = ""
    For Each dir As String In System.IO.Directory.GetDirectories("C:\Program Files")

        y += dir & " : " & CStr(dir.Count)

    Next


    Label1.Text = y
End Sub

Upvotes: 0

Related Questions