CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 1999
    Location
    Colombo, Sri Lanka
    Posts
    27

    Directories & Files

    Hi,

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

    Thanks,

    Rob


  2. #2
    Join Date
    May 1999
    Posts
    3,332

    Re: Directories & Files

    I'd loop through all files using the Dir function and sum the file size by means of the FileLen function.


  3. #3
    Join Date
    May 1999
    Posts
    3,332

    Re: Directories & Files

    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





  4. #4
    Guest

    Expand on this?

    How can you use that to find out the total size of a directory? Not just files in the immediate directory.


  5. #5
    Join Date
    Feb 2000
    Location
    Canada
    Posts
    8

    Re: Expand on this?

    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."

  6. #6
    Guest

    Re: Expand on this?

    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!


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

Featured