Results 1 to 11 of 11

Thread: Sizes of Files in a Directory [Resolved]

  1. #1

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575

    Sizes of Files in a Directory [Resolved]

    Hi,

    I have been having a lot of different attempts at this, and they all seem to fail.

    Is there anyway to quickly get the sizes of all the files within the directory and then add them together to get a file size of all of them?

    I need this for a quick feature in my app to get the size of the directory, not including the size of files within subfolders.

    Thanks for the help
    Last edited by LITHIA; May 14th, 2004 at 10:55 AM.

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    Something like this:

    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         MsgBox(Format(FolderSize("C:\"), "###,###,###"))
    3.     End Sub
    4.  
    5.     Public Function FolderSize(ByVal strFolder As String) As Long
    6.         Dim fs As IO.Directory
    7.         Dim myFile As IO.FileInfo
    8.         Dim sTemp() As String
    9.         Dim s As String
    10.         Dim lngLength As Long
    11.         lngLength = 0
    12.         sTemp = fs.GetFiles(strFolder)
    13.         For Each s In sTemp
    14.             myFile = New IO.FileInfo(s)
    15.             lngLength += myFile.Length()
    16.         Next
    17.         Return lngLength
    18.  
    19.     End Function

  3. #3

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    thanks! that worked great!

    Any idea of how i would convert the bytes into MB?

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    Divide by (1024*1024)

    VB Code:
    1. MsgBox(Format(FolderSize("C:\") / (1024 * 1024), "###,###,### MB"))

  5. #5

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    wow you're good! thanks! now, i swear this will be last question.

    Anyway to get it to 2 decimal places when I put it as a double?

    I got

    Dim FolderSizeMB As Double = FolderSize() / (1024 * 1024)

    which works fine, but gives 11 decimal places instead of the 1 that i want. Using a Long or Short just ends up giving the MB to the whole number, but 2 decimal places is good for an mb.

    Thanks very much again Negative0, you're great!

  6. #6
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    Code:
    Dim FolderSizeMB As Double = FolderSize() / (1024 * 1024)
    
    msgbox Format(FolderSizeMB,"###,###.00")

  7. #7
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    Or you could use the round function

    VB Code:
    1. Dim FolderSizeMB As Double = FolderSize("C:\") / (1024 * 1024)
    2.         FolderSizeMB = System.Math.Round(FolderSizeMB, 2)
    3.         MsgBox(FolderSizeMB)

  8. #8

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    anyway of converting the actual variable? I'm not displaying the size in a msgbox so i cant put a mask on it.

    im adding it into a listbox, like this

    lbInfo.Items.Add("Size of Directory: " & FolderSize() & " bytes, " & FolderSizeMB & " MB")

    So i need to specifically convert the variable

    thanks

  9. #9
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    Use the round function as mentioned above. You must have been typing your message when I posted it

  10. #10

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    ah yeah thanks! ill try it when i get back, lol

    You must have been typing your message when I posted it
    must have

    thanks very mucccchh

  11. #11

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    Heheh that worked fantastic! Thankyou so much for your help!

    Really appreciated,

    Thread Resolved

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