View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
RB Smissaert RB Smissaert is offline
external usenet poster
 
Posts: 2,452
Default counting excel files in a folder

This is how you can do it with the Windows API.
Haven't tested, but might be the fastest way.

Option Explicit

Private Const vbDot = 46
Private Const MAX_PATH = 260
Private Const INVALID_HANDLE_VALUE = -1
Private Const vbBackslash = "\"
Private Const ALL_FILES = "*.*"

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 FindClose Lib "kernel32" _
(ByVal hFindFile As Long) As Long
Private Declare Function FindFirstFile Lib "kernel32" _
Alias "FindFirstFileA" _
(ByVal lpFileName As String, _
lpFindFileData As WIN32_FIND_DATA)
As Long
Private Declare Function FindNextFile Lib "kernel32" _
Alias "FindNextFileA" _
(ByVal hFindFile As Long, _
lpFindFileData As WIN32_FIND_DATA) As
Long
Private Declare Function lstrlen Lib "kernel32" _
Alias "lstrlenW" (ByVal lpString As Long)
As Long

Function CountFilesInFolder(sRoot As String, _
strFile As String) As Long

Dim WFD As WIN32_FIND_DATA
Dim hFile As Long
Dim strFileName As String

hFile = FindFirstFile(sRoot & ALL_FILES, WFD)

If hFile < INVALID_HANDLE_VALUE Then
Do
'if a folder call method again
If (WFD.dwFileAttributes And vbDirectory) Then
If Asc(WFD.cFileName) < vbDot Then
'--------------------
'DEALING WITH FOLDERS
'--------------------
CountFilesInFolder _
sRoot & TrimNull(WFD.cFileName) & vbBackslash, _
strFile
End If
Else
'------------------
'DEALING WITH FILES
'------------------
strFileName = TrimNull(WFD.cFileName)
If strFileName Like strFile Then
CountFilesInFolder = CountFilesInFolder + 1
End If
End If
Loop While FindNextFile(hFile, WFD)
End If

FindClose hFile

End Function

Function TrimNull(strString As String) As String
TrimNull = Left$(strString, lstrlen(StrPtr(strString)))
End Function


RBS



"dstiefe" wrote in message
...
I have the file path...

now I need to count the number of xcel files