Click to See Complete Forum and Search --> : Directories & Files


Robert Fernando
November 4th, 1999, 10:19 PM
Hi,

How does one determine the number of files in a directory and the size (MB) of that directory?

Thanks,

Rob

Lothar Haensler
November 5th, 1999, 01:39 AM
I'd loop through all files using the Dir function and sum the file size by means of the FileLen function.

Lothar Haensler
November 5th, 1999, 01:55 AM
e.g.


private Sub Command1_Click()
Dim iCount as Integer
Dim lSize as Long
iCount = GetDirCount("c:\winnt", lSize)
MsgBox iCount & " files; " & lSize & " BYtes"
End Sub

Function GetDirCount(byval strPath as string, byref lSize as Long) as Integer
lSize = 0
Dim iCount as Integer
Dim strFile as string
If Right(strPath, 1) <> "\" then strPath = strPath & "\"
strFile = Dir(strPath & "*.*")
Do While strFile <> ""
iCount = iCount + 1
strFile = strPath & strFile
lSize = lSize + FileLen(strFile)
strFile = Dir
Loop
GetDirCount = iCount
End Function

April 3rd, 2000, 12:22 PM
How can you use that to find out the total size of a directory? Not just files in the immediate directory.

SGSCompany
April 4th, 2000, 08:27 AM
I don't know why nobody has answered this yet, so here it is:


private Function GetFolderSize(folderpath) as Double
Dim fs, f, s
set fs = CreateObject("Scripting.FileSystemObject")
set f = fs.GetFolder(folderpath)
on error resume next
GetFolderSize = f.Size
End Function




It's not that hard, is it? I hope that's what you wanted.

"I'm not biased, I'm Christian."

April 10th, 2000, 12:18 PM
THANKS!!! That's exactly what I wanted! Not this crap about listing files. It seems like you are the only one who can understand English. Thanks again!