Results 1 to 22 of 22

Thread: checking dir # of files and SIZE of dir how?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2006
    Posts
    65

    checking dir # of files and SIZE of dir how?

    I want to be able to check the numbers of files in a directory and the directories/folders total size.

    I have a code that will check file size
    but if i change the file into c:\folder\*.* I.E i get an error , and i tryed it without the *.* , same thing,

    any ideas will be apreciated

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: checking dir # of files and SIZE of dir how?

    Try this. Change the folder name as appropriate.
    vb Code:
    1. Option Explicit
    2.  
    3. Private Const MAX_PATH = 260
    4.  
    5. Private Type FILETIME
    6.     dwLowDateTime As Long
    7.     dwHighDateTime As Long
    8. End Type
    9. Private Type WIN32_FIND_DATA
    10.     dwFileAttributes As Long
    11.     ftCreationTime As FILETIME
    12.     ftLastAccessTime As FILETIME
    13.     ftLastWriteTime As FILETIME
    14.     nFileSizeHigh As Long
    15.     nFileSizeLow As Long
    16.     dwReserved0 As Long
    17.     dwReserved1 As Long
    18.     cFileName As String * MAX_PATH
    19.     cAlternate As String * 14
    20. End Type
    21.  
    22. Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" _
    23. (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
    24.  
    25. Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" _
    26. (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
    27.  
    28. Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
    29.  
    30. Private Function EndSlash(ByVal PathIn As String) As String
    31. If Right$(PathIn, 1) = "\" Then
    32.     EndSlash = PathIn
    33. Else
    34.     EndSlash = PathIn & "\"
    35. End If
    36. End Function
    37.  
    38. Private Function SizeOf(ByVal DirPath As String) As Double
    39. Dim hFind As Long
    40. Dim fdata As WIN32_FIND_DATA
    41. Dim dblSize As Double
    42. Dim sName As String
    43. Dim x As Long
    44. On Error Resume Next
    45. x = GetAttr(DirPath)
    46. If Err Then SizeOf = 0: Exit Function
    47.  
    48. If (x And vbDirectory) = vbDirectory Then
    49.     dblSize = 0
    50.     Err.Clear
    51.     sName = Dir$(EndSlash(DirPath) & "*.*", vbSystem Or vbHidden Or vbDirectory)
    52.         If Err.Number = 0 Then
    53.            hFind = FindFirstFile(EndSlash(DirPath) & "*.*", fdata)
    54.                If hFind = 0 Then Exit Function
    55.                 Do
    56.                   If (fdata.dwFileAttributes And vbDirectory) = vbDirectory Then
    57.                     sName = Left$(fdata.cFileName, InStr(fdata.cFileName, vbNullChar) - 1)
    58.                        If sName <> "." And sName <> ".." Then
    59.                         dblSize = dblSize + SizeOf(EndSlash(DirPath) & sName)
    60.                        End If
    61.                   Else
    62.                     dblSize = dblSize + fdata.nFileSizeHigh * 65536 + fdata.nFileSizeLow
    63.                   End If
    64.                 DoEvents
    65.                 Loop While FindNextFile(hFind, fdata) <> 0
    66.                 hFind = FindClose(hFind)
    67.         End If
    68. Else
    69.     dblSize = FileLen(DirPath)
    70. End If
    71. SizeOf = dblSize
    72. End Function
    73.  
    74. Private Sub Command1_Click()
    75. Screen.MousePointer = vbHourglass
    76. Dim dblFolderSize As Double
    77. dblFolderSize = SizeOf("d:\documents and settings")
    78. MsgBox Format(dblFolderSize, "#,##0;(#,##0)")
    79. Screen.MousePointer = vbDefault
    80. End Sub

  3. #3
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: checking dir # of files and SIZE of dir how?

    Consider this:
    Code:
    Dim FileName As String, Bytes As Long, FileCount As Integer
    Private Sub Form_Load()
    FileName = Dir("*.*")
    While Len(FileName)
        FileCount = FileCount + 1
        Bytes = Bytes + FileLen(FileName)
        FileName = Dir()
    Wend
    MsgBox Str(FileCount) & " files on subdirectory using" & Str(Bytes) & " bytes."
    End Sub
    This seems to work well for the immediate subdirectory and that may be all that you need. Change the path to get another one.
    Doctor Ed

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2006
    Posts
    65

    Re: checking dir # of files and SIZE of dir how?

    the first one doeent wokr , it has errors

    the second one cant be set to do other directions except the one that the EXE is in..

    like if i chaneg the "*.*" into "c:\*.*" wont work , said file not found .

  5. #5
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: checking dir # of files and SIZE of dir how?

    So you want to get the number of files in the root folder and all the sub folders? As well as the total file size? Please clarify.

  6. #6
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: checking dir # of files and SIZE of dir how?

    You can also use FileSystemObject - not the fastest method but definitely the simplest:
    Code:
    Private Sub Command1_Click()
    '============================
    Dim fso As Object
    Dim drv As Object
    
        Screen.MousePointer = vbHourglass
        
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set drv = fso.GetDrive(fso.GetDriveName("c:"))
        
        Debug.Print "Drive's total size: " & FormatNumber(drv.TotalSize / 1024, 0) & " KB"
        Debug.Print "Drive's available space: " & FormatNumber(drv.AvailableSpace / 1024, 0) & " KB"
        Debug.Print "Drive's free space: " & FormatNumber(drv.FreeSpace / 1024, 0) & " KB"
        Debug.Print "Total subfolders: " & drv.RootFolder.SubFolders.Count
        
        Screen.MousePointer = vbDefault
    
    End Sub

  7. #7
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    261

    Re: checking dir # of files and SIZE of dir how?

    i can you please post hoe to change bytes to Mb and gig

  8. #8

  9. #9
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: checking dir # of files and SIZE of dir how?

    Quote Originally Posted by c_owl
    i can you please post hoe to change bytes to Mb and gig
    Here is some code given to me by Ellis Dee from these forums. It accepts the file size as currency to handle large sizes (> 2GB, I think up to around 920 Terabytes).

    You may or may not need to use the CCur() function to convert the long value to currency when using it, ie:

    vb Code:
    1. Dim lonTotalSize As Long
    2.  
    3. lonTotalSize = SizeOfDirectory
    4. lblSize.Caption = FormatSize(CCur(lonTotalSize))

    Here is the code:
    Code:
    Public Function FormatSize(ByVal Size As Currency) As String
        Const Kilobyte As Currency = 1024@
        Const HundredK As Currency = 102400@
        Const ThousandK As Currency = 1024000@
        Const Megabyte As Currency = 1048576@
        Const HundredMeg As Currency = 104857600@
        Const ThousandMeg As Currency = 1048576000@
        Const Gigabyte As Currency = 1073741824@
        Const Terabyte As Currency = 1099511627776@
        
        If Size < Kilobyte Then
            FormatSize = Int(Size) & " bytes"
        ElseIf Size < HundredK Then
            FormatSize = Format(Size / Kilobyte, "#.0") & " KB"
        ElseIf Size < ThousandK Then
            FormatSize = Int(Size / Kilobyte) & " KB"
        ElseIf Size < HundredMeg Then
            FormatSize = Format(Size / Megabyte, "#.0") & " MB"
        ElseIf Size < ThousandMeg Then
            FormatSize = Int(Size / Megabyte) & " MB"
        ElseIf Size < Terabyte Then
            FormatSize = Format(Size / Gigabyte, "#.00") & " GB"
        Else
            FormatSize = Format(Size / Terabyte, "#.00") & " TB"
        End If
    End Function

  10. #10
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: checking dir # of files and SIZE of dir how?

    If you use rhinobull's code, you will need to add a reference to the filesystem object.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  11. #11
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: checking dir # of files and SIZE of dir how?

    Well, if you look at my sample code you will find that it's using late bindings so no references are needed at all.
    However scripting library is required but since it's a major component since Win98 we are safe.

  12. #12
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    261

    Re: checking dir # of files and SIZE of dir how?

    Quote Originally Posted by DigiRev
    Here is some code given to me by Ellis Dee from these forums. It accepts the file size as currency to handle large sizes (> 2GB, I think up to around 920 Terabytes).

    You may or may not need to use the CCur() function to convert the long value to currency when using it, ie:

    vb Code:
    1. Dim lonTotalSize As Long
    2.  
    3. lonTotalSize = SizeOfDirectory
    4. lblSize.Caption = FormatSize(CCur(lonTotalSize))

    Here is the code:
    Code:
    Public Function FormatSize(ByVal Size As Currency) As String
        Const Kilobyte As Currency = 1024@
        Const HundredK As Currency = 102400@
        Const ThousandK As Currency = 1024000@
        Const Megabyte As Currency = 1048576@
        Const HundredMeg As Currency = 104857600@
        Const ThousandMeg As Currency = 1048576000@
        Const Gigabyte As Currency = 1073741824@
        Const Terabyte As Currency = 1099511627776@
        
        If Size < Kilobyte Then
            FormatSize = Int(Size) & " bytes"
        ElseIf Size < HundredK Then
            FormatSize = Format(Size / Kilobyte, "#.0") & " KB"
        ElseIf Size < ThousandK Then
            FormatSize = Int(Size / Kilobyte) & " KB"
        ElseIf Size < HundredMeg Then
            FormatSize = Format(Size / Megabyte, "#.0") & " MB"
        ElseIf Size < ThousandMeg Then
            FormatSize = Int(Size / Megabyte) & " MB"
        ElseIf Size < Terabyte Then
            FormatSize = Format(Size / Gigabyte, "#.00") & " GB"
        Else
            FormatSize = Format(Size / Terabyte, "#.00") & " TB"
        End If
    End Function
    im using the above code to try and format drive and file bytes, but i get error
    MUST SPECIFY INDEX FOR OBJECT ARRAY
    im usung the code below.. please help ive been at it 2 days and loosing my mind
    Code:
    Set fso = CreateObject("Scripting.FileSystemObject")
        Set drv = fso.GetDrive(fso.GetDriveName((Mid(drive.Caption, 1, 2))))
       Debug.Print "-------------------------------"
       Debug.Print FormatSize(CCur(drv.AvailableSpace))' this returns error
         Debug.Print "______________"
        StatusBar1.Panels(1) = "Drive's total: " & FormatNumber(drv.TotalSize / 1024, 0) & " KB"
         StatusBar1.Panels(2) = "Drive space: " & FormatNumber(drv.AvailableSpace / 1024, 0) & " KB"

  13. #13
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: checking dir # of files and SIZE of dir how?

    Quote Originally Posted by c_owl
    im using the above code to try and format drive and file bytes, but i get error
    MUST SPECIFY INDEX FOR OBJECT ARRAY
    im usung the code below.. please help ive been at it 2 days and loosing my mind
    Code:
    Set fso = CreateObject("Scripting.FileSystemObject")
        Set drv = fso.GetDrive(fso.GetDriveName((Mid(drive.Caption, 1, 2))))
       Debug.Print "-------------------------------"
       Debug.Print FormatSize(CCur(drv.AvailableSpace))' this returns error
         Debug.Print "______________"
        StatusBar1.Panels(1) = "Drive's total: " & FormatNumber(drv.TotalSize / 1024, 0) & " KB"
         StatusBar1.Panels(2) = "Drive space: " & FormatNumber(drv.AvailableSpace / 1024, 0) & " KB"
    Hmmm. The FormatSize() function doesn't use any objects or arrays, so I'm thinking it has something to do with the fso stuff. (I never use fso, so I'm just guessing.)

    Try this code and post what happens:
    Code:
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set drv = fso.GetDrive(fso.GetDriveName((Mid(drive.Caption, 1, 2))))
        Debug.Print "-------------------------------"
    
        Dim curDebug As Currency
        curDebug = CCur(drv.AvailableSpace)
    
        Debug.Print FormatSize(curDebug)
        Debug.Print "______________"

  14. #14
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: checking dir # of files and SIZE of dir how?

    is this the late binding?
    Set fso = CreateObject("Scripting.FileSystemObject")
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  15. #15

  16. #16
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    261

    Unhappy Re: checking dir # of files and SIZE of dir how?

    Quote Originally Posted by Ellis Dee
    Hmmm. The FormatSize() function doesn't use any objects or arrays, so I'm thinking it has something to do with the fso stuff. (I never use fso, so I'm just guessing.)

    Try this code and post what happens:
    Code:
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set drv = fso.GetDrive(fso.GetDriveName((Mid(drive.Caption, 1, 2))))
        Debug.Print "-------------------------------"
    
        Dim curDebug As Currency
        curDebug = CCur(drv.AvailableSpace)
    
        Debug.Print FormatSize(curDebug)
        Debug.Print "______________"
    hi
    i have tryed what u poster but still came back with error array
    the error is in
    FormatSize = Format(Size / Gigabyte, "#.00") & " GB"

  17. #17
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: checking dir # of files and SIZE of dir how?

    Are you using Option Explicit? If so, what is drive defined as?

    Here's something to get you pointed in the right direction: Create a blank project, copy the following code into the form's code window, and then run:
    Code:
    Option Explicit
    
    Private Sub Form_Load()
        Dim fso As Object
        Dim drv As Object
        Dim curDebug As Currency
        
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set drv = fso.GetDrive("c:")
        
        curDebug = CCur(drv.AvailableSpace)
        
        MsgBox FormatSize(curDebug)
        Set drv = Nothing
        Set fso = Nothing
    End Sub
    
    Public Function FormatSize(ByVal Size As Currency) As String
        Const Kilobyte As Currency = 1024@
        Const HundredK As Currency = 102400@
        Const ThousandK As Currency = 1024000@
        Const Megabyte As Currency = 1048576@
        Const HundredMeg As Currency = 104857600@
        Const ThousandMeg As Currency = 1048576000@
        Const Gigabyte As Currency = 1073741824@
        Const Terabyte As Currency = 1099511627776@
        
        If Size < Kilobyte Then
            FormatSize = Int(Size) & " bytes"
        ElseIf Size < HundredK Then
            FormatSize = Format(Size / Kilobyte, "#.0") & " KB"
        ElseIf Size < ThousandK Then
            FormatSize = Int(Size / Kilobyte) & " KB"
        ElseIf Size < HundredMeg Then
            FormatSize = Format(Size / Megabyte, "#.0") & " MB"
        ElseIf Size < ThousandMeg Then
            FormatSize = Int(Size / Megabyte) & " MB"
        ElseIf Size < Terabyte Then
            FormatSize = Format(Size / Gigabyte, "#.00") & " GB"
        Else
            FormatSize = Format(Size / Terabyte, "#.00") & " TB"
        End If
    End Function

  18. #18
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    261

    Exclamation Re: checking dir # of files and SIZE of dir how?

    hi
    this is fantastice thanks for the help.
    its run great on it own but when i input it to my program is returns a error
    run-time error !344!
    must specify index for object array
    i ran it in its own mobula because thats how i ran it on its own

  19. #19
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: checking dir # of files and SIZE of dir how?

    I don't know what more I can do to help without seeing the project. Can you zip it up and attach it? (Please don't use rar; I can't read rar archives.)

  20. #20
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    261

    Re: checking dir # of files and SIZE of dir how?

    my program has a lot to g yet I'm going to post it when I have done it, because of all the help I have had from everyone.
    I have got around it by doing something else,

    my program is a database witch saves a your videos on mediacentre and all covers. it also allows you to manage hardrive space, that's what I'm working on now.
    I'm also going to put a video packer/cd writer into the program my program is detailed so I've also started doing a help web page for it,

    thanks for all your help

  21. #21
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: checking dir # of files and SIZE of dir how?

    GOOD LUCK with the cd-writer. You will see what i mean...
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  22. #22
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    261

    Angry Re: checking dir # of files and SIZE of dir how?

    Code:
    Set fso = CreateObject("Scripting.FileSystemObject")
        Set drv = fso.GetDrive(fso.GetDriveName((Mid(drive.Caption, 1, 2))))
        Debug.Print "-------------------------------"
    
        Dim curDebug As Currency
        curDebug = CCur(drv.AvailableSpace)
    dd=FileLen(Format.Label2.Caption + Format.Label1.Caption)
    msgbox dd - curdebug


    hi
    im trying to create some code that will check if a drive has more space than the file im copying to the drive. if he file is larger than the space on the drive then returns error message.
    has any tryed this as i cannot get it to work. i have tryed using the above to return the the file and drive size witch is ok but i cannot work out how to compaire them

    any thought would be great

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