Results 1 to 3 of 3

Thread: get directory size

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 1999
    Location
    Eugene, OR, USA
    Posts
    1

    Post

    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.

  2. #2
    Junior Member
    Join Date
    Nov 1999
    Posts
    27

    Post

    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.

  3. #3
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    Try using the FileSystemObject it means having to include the Scripting Runtime File(s).:

    In a Form with a Textbox and CommandButton..
    Code:
    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
    [email protected]
    [email protected]

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