|
-
May 13th, 2004, 12:06 PM
#1
Thread Starter
Fanatic Member
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.
-
May 13th, 2004, 12:13 PM
#2
Something like this:
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MsgBox(Format(FolderSize("C:\"), "###,###,###"))
End Sub
Public Function FolderSize(ByVal strFolder As String) As Long
Dim fs As IO.Directory
Dim myFile As IO.FileInfo
Dim sTemp() As String
Dim s As String
Dim lngLength As Long
lngLength = 0
sTemp = fs.GetFiles(strFolder)
For Each s In sTemp
myFile = New IO.FileInfo(s)
lngLength += myFile.Length()
Next
Return lngLength
End Function
-
May 13th, 2004, 12:30 PM
#3
Thread Starter
Fanatic Member
thanks! that worked great!
Any idea of how i would convert the bytes into MB?
-
May 13th, 2004, 12:31 PM
#4
Divide by (1024*1024)
VB Code:
MsgBox(Format(FolderSize("C:\") / (1024 * 1024), "###,###,### MB"))
-
May 13th, 2004, 12:49 PM
#5
Thread Starter
Fanatic Member
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!
-
May 13th, 2004, 12:54 PM
#6
Code:
Dim FolderSizeMB As Double = FolderSize() / (1024 * 1024)
msgbox Format(FolderSizeMB,"###,###.00")
-
May 13th, 2004, 12:58 PM
#7
Or you could use the round function
VB Code:
Dim FolderSizeMB As Double = FolderSize("C:\") / (1024 * 1024)
FolderSizeMB = System.Math.Round(FolderSizeMB, 2)
MsgBox(FolderSizeMB)
-
May 13th, 2004, 12:59 PM
#8
Thread Starter
Fanatic Member
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
-
May 13th, 2004, 01:02 PM
#9
Use the round function as mentioned above. You must have been typing your message when I posted it
-
May 14th, 2004, 05:53 AM
#10
Thread Starter
Fanatic Member
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
-
May 14th, 2004, 10:57 AM
#11
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|