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
Printable View
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
VB Code:
Option Explicit Private Const MAX_PATH = 260 Private Const INVALID_HANDLE_VALUE = -1 Private Const FILE_ATTRIBUTE_DIRECTORY = &H10 Private Type FILETIME dwLowDateTime As Long dwHighDateTime As Long End Type Private Type WIN32_FIND_DATA dwFileAttributes As Long ftCreationTime As FILETIME ftLastAccessTime As FILETIME ftLastWriteTime As FILETIME nFileSizeHigh As Long nFileSizeLow As Long dwReserved0 As Long dwReserved1 As Long cFileName As String * MAX_PATH cAlternate As String * 14 End Type Private Declare Function FindFirstFile Lib "kernel32.dll" Alias "FindFirstFileA" _ (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long Private Declare Function FindNextFile Lib "kernel32.dll" Alias "FindNextFileA" _ (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long Private Declare Function FindClose Lib "kernel32.dll" (ByVal hFindFile As Long) As Long Private Function NumFiles(pstrPath As String) As Long Dim Find_Data As WIN32_FIND_DATA Dim lngFile As Long NumFiles = 0 If Right(pstrPath, 1) <> "\" Then pstrPath = pstrPath & "\" pstrPath = pstrPath & "*.*" lngFile = FindFirstFile(pstrPath, Find_Data) If lngFile = INVALID_HANDLE_VALUE Then Exit Function If (Find_Data.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = 0 Then NumFiles = 1 Do While FindNextFile(lngFile, Find_Data) If (Find_Data.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = 0 Then NumFiles = NumFiles + 1 Loop FindClose (lngFile) End Function Private Sub Command1_Click() MsgBox "There are " & NumFiles("c:\mailalert:\") & " Files in C:\MailAlert" End Sub
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
:thumb: I forgot you could use the File System Object to do this. Here is how you would do that same thing, using the FSO.Quote:
Originally Posted by westconn1
VB Code:
Set A Reference To FileSystemObject Private Function FileCount(ByVal FolderName As String) As Long Dim objFS As New Scripting.FileSystemObject Dim objFolder As Scripting.Folder If objFS.FolderExists(FolderName) Then Set objFolder = objFS.GetFolder(FolderName) 'if this lines returns more than 0, the folder has files FileCount = objFolder.Files.Count End If Set objFolder = Nothing Set objFS = Nothing End Function Private Sub cmdCheckForFiles_Click() MsgBox FileCount("C:\MailAlert") End Sub