Get count of files in subfolders too using File System Object (or some other way?)
Is there a way to do this?
The objFolder.files.Count returns only the count
of files in that folder... is there a way to make it count files
in subfolders too?
Re: Get count of files in subfolders too using File System Object (or some other way?
Re: Get count of files in subfolders too using File System Object (or some other way?
You'd really be better off using the FindFirst/NextFile API
I adapted this from a generic search a while ago. Goes in a module.
Code:
Public Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, _
lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, _
lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, _
Source As Any, _
ByVal Length As Long)
Public Declare Function PathMatchSpec Lib "shlwapi" Alias "PathMatchSpecW" (ByVal pszFileParam As Long, _
ByVal pszSpec As Long) As Long
Public Type WIN32_FIND_DATA ' wfd
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
cAlternateFileName As String * 14
End Type
Public Type FILETIME ' ft
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Public Type FILE_PARAMS
bRecurse As Boolean
bFindOrExclude As Long '1=find matching, 0=exclude matching
nCount As Long
nSearched As Long
sFileNameExt As String
sFileRoot As String
nFileSize As Currency
End Type
Private fp1331 As FILE_PARAMS
Public Function FileSearch(sPath As String, sPattern As String, sOut() As String, Optional sSize As String, Optional bRec As Boolean = True) As Long
'wraps the SearchForFiles function-
'since that's recursive it adds to a module level search results list that must
'be reset before use and retrieved afterwards
If sPath = "" Then Exit Function
If sPath = " " Then Exit Function
If sPath = vbNullChar Then Exit Function
ReDim sSearchResults(0)
With fp1331
.sFileRoot = ZQualifyPath(sPath) 'start path
.sFileNameExt = sPattern 'file type(s) of interest
.bRecurse = bRec 'True = recursive search
.nCount = 0 'results
.nSearched = 0 'results
.nFileSize = 0
End With
Call SearchForFiles(fp1331.sFileRoot)
sOut = sSearchResults
sSize = CStr(fp1331.nFileSize)
FileSearch = fp1331.nCount
End Function
Private Sub SearchForFiles(sRoot As String)
Dim WFD As WIN32_FIND_DATA
Dim hFile As Long
hFile = FindFirstFile(sRoot & ALL_FILES, WFD)
If hFile <> INVALID_HANDLE_VALUE Then
Do
'if a folder, and recurse specified, call
'method again
If (WFD.dwFileAttributes And vbDirectory) Then
If Asc(WFD.cFileName) <> vbDot Then
If fp1331.bRecurse Then
SearchForFiles sRoot & ZTrimNullZ(WFD.cFileName) & vbBackslash
End If
End If
Else
'must be a file..
If ZMatchSpec(WFD.cFileName, fp1331.sFileNameExt) Then
fp1331.nCount = fp1331.nCount + 1
fp1331.nFileSize = fp1331.nFileSize + FileSizeWFD(WFD.nFileSizeHigh, WFD.nFileSizeLow)
'DebugAppend sRoot & ZTrimNullZ(WFD.cFileName)
strarr_AddItem sSearchResults, sRoot & ZTrimNullZ(WFD.cFileName)
End If 'If MatchSpec
End If 'If WFD.dwFileAttributes
fp1331.nSearched = fp1331.nSearched + 1
Loop While FindNextFile(hFile, WFD)
End If 'If hFile
Call FindClose(hFile)
End Sub
Private Function ZQualifyPath(sPath As String) As String
If Right$(sPath, 1) <> vbBackslash Then
ZQualifyPath = sPath & vbBackslash
Else
ZQualifyPath = sPath
End If
End Function
Private Function ZTrimNullZ(startstr As String) As String
ZTrimNullZ = Left$(startstr, lstrlenW(ByVal StrPtr(startstr)))
End Function
Public Function ZMatchSpec(sFile As String, sSpec As String) As Boolean
ZMatchSpec = PathMatchSpec(StrPtr(sFile), StrPtr(sSpec))
End Function
Public Sub strarr_AddItem(v() As String, sz As String, Optional idx As Long = -1)
'helper function to add an item to a string array
Dim i As Long, j As Long
i = UBound(v)
If idx > -1 Then
Dim h() As String
ReDim h(LBound(v) To (i + 1))
For j = LBound(h) To UBound(h)
If j < idx Then
h(j) = v(j)
ElseIf j = idx Then
h(j) = sz
Else 'j>idx
h(j) = v(j - 1)
End If
Next j
v = h
Else
If CStr(v(i)) = "" Then
v(i) = sz
Else
ReDim Preserve v(i + 1)
v(i + 1) = sz
End If
End If
End Sub
Function FileSizeWFD(FileSizeHigh&, FileSizeLow&) As Currency
Dim c As Currency, LArr(0 To 1) As Long
LArr(0) = FileSizeLow: LArr(1) = FileSizeHigh
CopyMemory c, LArr(0), 8
FileSizeWFD = c * 10000
End Function
I think I got all the API declares and helper functions.
Function FileSearch(sPath As String, sPattern As String, sOut() As String, Optional sSize As String, Optional bRec As Boolean = True) As Long
-sPath is the starting folder, sPattern should be * if you want all files, sSize is an output that receives the file size (you can switch it to the currency type and remove the cstr if you don't need the string), bRec should be true- makes the search recursive (search all subfolders). The return value is the number of files found. sOut() is an actual list of the files; you can remove that and the line that fills it if you don't need it.
Re: Get count of files in subfolders too using File System Object (or some other way?
The API is a good choice
Dir$() will also work well, either is better than FSO
You need to use recursion to get all of the subfolders no matter which method you use.
You mention the size property but it is safe to say that uses recursion behind the scenes to get the size of the data in all the sub folders as well. It also takes a while when dealing with lots of files in multiple subfolders
Re: Get count of files in subfolders too using File System Object (or some other way?
Quote:
Originally Posted by
DataMiser
You need to use recursion to get all of the subfolders no matter which method you use.
Recursion isn't necessary at all, though it is often used for this even by those who don't understand the concept. Must be a lot of copy/paste cowboys out there. ;)
See DirLister lightweight Dir() wrapper which will descend the directory tree without any recursive calls. Should be a trivial enough matter to dumb it down to just return counts.
Re: Get count of files in subfolders too using File System Object (or some other way?
I think baja_yu would have found an answer after 10 years.
Re: Get count of files in subfolders too using File System Object (or some other way?
Quote:
Originally Posted by
Nightwalker83
I think baja_yu would have found an answer after 10 years.
LOL.. I did not even notice the date of the OP