PDA

Click to See Complete Forum and Search --> : get directory size


garyhair
Nov 12th, 1999, 05:56 AM
I am creating an app that will send me an email when a directory gets filled to a certain size. I have found many ways to get free space or file size, but nothing for total files an a specific directory. I could probably step through the directory getting all of the files and sized then add them, but thought there must be an easier way.

clefus
Nov 12th, 1999, 08:16 AM
It's the same principle:

Private Sub cmdOK_Click()

Dim PathAndName As String
Dim FileSize As String
Dim Path As String

' If no file is selected, tell the user and exit this procedure.
If txtFileName.text = "" then
MsgBox "You must first select a file!"
Exit Sub
End if

' Make sure that Path ends with a \. (\)
If Right(filfiles.Path, 1) <> "\" Then
Path = filfiles.path + "\"
Else
Path = filfiles.Path
End if

'Extract the PAth and name of the selected file.
If txtFileName.Text = filfiles.Filename Then
PathAndName = Path + filfiles.FileName
Else
PathAndName = txtFileName.Text
End If

'The next statement may cause an error, so we set an error trap.
On error Foto FileLenError

'Get the file size of the file.
FileSize = Str(FileLen(PathAndName))

'Display the size of the file.
MsgBox "Size of "+PathAndName+": "FileSize+" bytes"
Exit Sub

FileLenError:
'There was an error, so display error message and exit.
MsgBox " Cannot find size of " + PathAndName
Exit sub
End Sub

Play around with this code...You should be able to change FileSize to DirSize or DirecotrySize. If not you'll have to run a loop finding all of the files then tell the file size of each and add them together.

Aaron Young
Nov 12th, 1999, 10:52 AM
Try using the FileSystemObject it means having to include the Scripting Runtime File(s).:

In a Form with a Textbox and CommandButton..

Private Sub Command1_Click()
Dim oFSO As Object
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFSO = oFSO.GetFolder(Text1)
Screen.MousePointer = vbHourglass
Caption = Format(oFSO.Size, "#,#") & " bytes."
Screen.MousePointer = vbDefault
Set oFSO = Nothing
End Sub



------------------
Aaron Young
Analyst Programmer
aarony@redwingsoftware.com
adyoung@win.bright.net