Click here to Skip to main content
15,915,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There is an ListBox in the form then

I am listing Folders Way in Listbox With FolderBrowserDialog as this ;

C:\Users\Yaman\Desktop\TestA '(Has 4 Folders 27 Files)
C:\Users\Yaman\Desktop\TestB '(Has 5 Folders 30 Files)

I can get the folders and files count when I listed one bye one.

if I listed only this ;

C:\Users\Yaman\Desktop\TestA

in listbox the result is
27 Files / 4 Folders

but when I listed both folder way I can get only last listed folders count result

if I listed

C:\Users\Yaman\Desktop\TestA
C:\Users\Yaman\Desktop\TestA

then result is last listed as this

30 Files / 5 Folders


How can I get all folders files and subfolders count which together listed in listbox ?

What I have tried:

VB
<pre>Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
        For z = 0 To ListBox1.Items.Count - 1
            Dim TheSize As Long = GetDirSize(ListBox1.Items.Item(z)) '("C:\Users\Yaman\Desktop\Deneme")
            Dim counterDir As Integer = IO.Directory.GetDirectories(ListBox1.Items.Item(z), "*.*", IO.SearchOption.AllDirectories).Length 'counts the number of files
            Dim counterFile As Integer = IO.Directory.GetFiles(ListBox1.Items.Item(z), "*.*", IO.SearchOption.AllDirectories).Count 'counts the number of files
            Label5.Text = counterFile & " Files" & " / " & counterDir.ToString & " Folders"
        Next z
       
    End Sub
Posted
Updated 15-Feb-19 9:46am

1 solution

Check this:

VB
Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
        Dim currpath As String = ""
        Dim TheSize As Long = 0
        Dim counterFile As Integer = 0
        Dim counterDir As Integer = 0
        
        For z = 0 To ListBox1.Items.Count - 1
            currpath = ListBox1.Items.Item(z).ToString()
            TheSize = GetDirSize(currpath) '("C:\Users\Yaman\Desktop\Deneme")
            counterDir += IO.Directory.GetDirectories(currpath, "*.*", IO.SearchOption.AllDirectories).Length 'counts the number of files
            counterFile += IO.Directory.GetFiles(currpath, "*.*", IO.SearchOption.AllDirectories).Count 'counts the number of files
        Next z
       Label5.Text = counterFile & " file(s)" & " in " & counterDir.ToString & " folder(s)"
       
    End Sub


Take a deep look at the changes ;)

For further details, please see: Scope in Visual Basic | Microsoft Docs[^]
 
Share this answer
 
v3
Comments
Nicomendox 16-Feb-19 10:59am    
Thank you it works great. And Sorry for my bad english.

with your permission I want to ask another question. as you see I try to copy multiple file(s) and Folder(s) which listed in listbox.

wtih this code I can also add multiple files in listbox :

Hide Copy Code
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then

Dim sayi As Integer = Me.OpenFileDialog1.FileNames.Length - 1
For i As Integer = 0 To sayi

ListBox1.Items.Add(OpenFileDialog1.FileNames(i))
'ListBox2.Items.Add(OpenFileDialog1.FileNames(i))
Next

End If


But...
When I add Multi Folders and Multi Files in a Listbox then this time I cant get the folders and Files Count together.

When I copy folder(s) and file(s) which listed in listbox I can copy all files and folder into torget directory well. but I cant count folders and files together

if you let me to ask how can I do it please ?


again thank you soo much for fast and exact solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900