Results 1 to 11 of 11

Thread: How can I Get Sum Total Size of Directories Which Listed in ListBox?

Hybrid View

  1. #1
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: How can I Get Sum Total Size of Directories Which Listed in ListBox?

    I didn’t just get one directory size. I put all of the directory names from my documents folder in my listbox, then run the exact same code I gave you...

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Nov 2018
    Posts
    77

    Re: How can I Get Sum Total Size of Directories Which Listed in ListBox?

    Quote Originally Posted by .paul. View Post
    I didn’t just get one directory size. I put all of the directory names from my documents folder in my listbox, then run the exact same code I gave you...
    I mean to say same. You put only Documents Folder. please put difrent folders (D:\XXX, or C:\Pictures and try again.)

    Not try only with one folder inside listbox. Try your code with multiple folders...

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: How can I Get Sum Total Size of Directories Which Listed in ListBox?

    Code:
    ListBox1.Items.AddRange(IO.Directory.GetDirectories("Z:\Documents"))
    He did have multiple directories in the list.
    The above line adds all the directories that are in Z:\Documents to the Listbox (note "AddRange" and "GetDirectories". GetDirectories returns an array of directory paths and AddRange adds all the elements of the array to the list.

    The code then gets the size of each of the directories (and the subdirectories below those directories) and gives the sum.

    That is what you said you wanted.

    A test more similar to your posts.
    Code:
     Public Class Form1
    
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        ListBox1.Items.Add("C:\users\passel\Desktop\Tools")  '7.5 MB
        ListBox1.Items.Add("C:\users\passel\Desktop\DeskTop Collection 2")  '691 MB
        ListBox1.Items.Add("C:\users\passel\Desktop\DeskTopCollection")  ' 121 MB
    
        Dim dirSize As Long = ListBox1.Items.Cast(Of String).Sum(Function(s) (From strFile In My.Computer.FileSystem.GetFiles(s, FileIO.SearchOption.SearchAllSubDirectories) Select New IO.FileInfo(strFile).Length).Sum())
        MsgBox((dirSize / 1024) / 1024) 'MB
      End Sub
    End Class
    The message box shows the total 820.675497..... so matches the expected size of the three directories sizes (as noted in the comments).

    I just removed one of the "/ 1024" so it is in MB rather than GB.

    You said it is working, so I assume it is good for you. I guess you just assumed he was adding only one directory to the Listbox, rather than all the directories from the Z:\Documents folder.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Nov 2018
    Posts
    77

    Re: How can I Get Sum Total Size of Directories Which Listed in ListBox?

    Quote Originally Posted by passel View Post
    Code:
    ListBox1.Items.AddRange(IO.Directory.GetDirectories("Z:\Documents"))
    He did have multiple directories in the list.
    The above line adds all the directories that are in Z:\Documents to the Listbox (note "AddRange" and "GetDirectories". GetDirectories returns an array of directory paths and AddRange adds all the elements of the array to the list.

    The code then gets the size of each of the directories (and the subdirectories below those directories) and gives the sum.

    That is what you said you wanted.

    A test more similar to your posts.
    Code:
     Public Class Form1
    
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        ListBox1.Items.Add("C:\users\passel\Desktop\Tools")  '7.5 MB
        ListBox1.Items.Add("C:\users\passel\Desktop\DeskTop Collection 2")  '691 MB
        ListBox1.Items.Add("C:\users\passel\Desktop\DeskTopCollection")  ' 121 MB
    
        Dim dirSize As Long = ListBox1.Items.Cast(Of String).Sum(Function(s) (From strFile In My.Computer.FileSystem.GetFiles(s, FileIO.SearchOption.SearchAllSubDirectories) Select New IO.FileInfo(strFile).Length).Sum())
        MsgBox((dirSize / 1024) / 1024) 'MB
      End Sub
    End Class
    The message box shows the total 820.675497..... so matches the expected size of the three directories sizes (as noted in the comments).

    I just removed one of the "/ 1024" so it is in MB rather than GB.

    You said it is working, so I assume it is good for you. I guess you just assumed he was adding only one directory to the Listbox, rather than all the directories from the Z:\Documents folder.

    Yes. its working. thanks a lot.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width