Results 1 to 4 of 4

Thread: count of files in a directory

  1. #1

    Thread Starter
    Banned
    Join Date
    Mar 2005
    Posts
    29

    count of files in a directory

    How do I simply get the count of the number of files in a directory?

    for instance if I have a directory c:\MailAlert
    and it has 3 files in it..I just want to retrieve the number 3.

    TY

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

    Re: count of files in a directory

    VB Code:
    1. Option Explicit
    2.  
    3. Private Const MAX_PATH = 260
    4. Private Const INVALID_HANDLE_VALUE = -1
    5. Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
    6.  
    7.  
    8. Private Type FILETIME
    9.     dwLowDateTime As Long
    10.     dwHighDateTime As Long
    11. End Type
    12.  
    13. Private Type WIN32_FIND_DATA
    14.     dwFileAttributes As Long
    15.     ftCreationTime As FILETIME
    16.     ftLastAccessTime As FILETIME
    17.     ftLastWriteTime As FILETIME
    18.     nFileSizeHigh As Long
    19.     nFileSizeLow As Long
    20.     dwReserved0 As Long
    21.     dwReserved1 As Long
    22.     cFileName As String * MAX_PATH
    23.     cAlternate As String * 14
    24. End Type
    25.  
    26.  
    27. Private Declare Function FindFirstFile Lib "kernel32.dll" Alias "FindFirstFileA" _
    28. (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
    29. Private Declare Function FindNextFile Lib "kernel32.dll" Alias "FindNextFileA" _
    30. (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
    31. Private Declare Function FindClose Lib "kernel32.dll" (ByVal hFindFile As Long) As Long
    32.  
    33. Private Function NumFiles(pstrPath As String) As Long
    34.     Dim Find_Data As WIN32_FIND_DATA
    35.     Dim lngFile As Long
    36.  
    37.     NumFiles = 0
    38.    
    39.     If Right(pstrPath, 1) <> "\" Then pstrPath = pstrPath & "\"
    40.     pstrPath = pstrPath & "*.*"
    41.    
    42.     lngFile = FindFirstFile(pstrPath, Find_Data)
    43.     If lngFile = INVALID_HANDLE_VALUE Then Exit Function
    44.    
    45.     If (Find_Data.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = 0 Then NumFiles = 1
    46.     Do While FindNextFile(lngFile, Find_Data)
    47.         If (Find_Data.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = 0 Then NumFiles = NumFiles + 1
    48.     Loop
    49.    
    50.     FindClose (lngFile)
    51. End Function
    52.  
    53. Private Sub Command1_Click()
    54.     MsgBox "There are " &  NumFiles("c:\mailalert:\") & " Files in C:\MailAlert"
    55. End Sub

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: count of files in a directory

    you can use the dir command to loop through the files with a counter

    alternately you could use fso which would see the files in a folder as a collection, which i would believe would have a count property

    pete

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

    Re: count of files in a directory

    Quote Originally Posted by westconn1
    you can use the dir command to loop through the files with a counter

    alternately you could use fso which would see the files in a folder as a collection, which i would believe would have a count property

    pete
    I forgot you could use the File System Object to do this. Here is how you would do that same thing, using the FSO.
    VB Code:
    1. Set A Reference To FileSystemObject
    2.  
    3. Private Function FileCount(ByVal FolderName As String) As Long
    4. Dim objFS As New Scripting.FileSystemObject
    5. Dim objFolder As Scripting.Folder
    6.  
    7. If objFS.FolderExists(FolderName) Then
    8.     Set objFolder = objFS.GetFolder(FolderName)
    9.     'if this lines returns more than 0, the folder has files
    10.     FileCount = objFolder.Files.Count
    11. End If
    12.  
    13. Set objFolder = Nothing
    14. Set objFS = Nothing
    15.  
    16. End Function
    17.  
    18. Private Sub cmdCheckForFiles_Click()
    19. MsgBox FileCount("C:\MailAlert")
    20. End Sub

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