Results 1 to 10 of 10

Thread: Get count of files in subfolders too using File System Object (or some other way?)

  1. #1

    Thread Starter
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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?
    Last edited by baja_yu; May 5th, 2004 at 03:52 PM.

  2. #2
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    You probably have to code it so it opens up each sub folder and counts them.

  3. #3

    Thread Starter
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989
    That will require a recursive function... I need to
    make the app as fast as possible... And stepping in all
    folders will slow my app down...
    Is there a way to make it work like the size property?
    The size gets the size of subfolders too...

  4. #4

    Thread Starter
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989
    Any other way except FSO?
    But it needs to be fast...

  5. #5
    New Member
    Join Date
    Oct 2014
    Posts
    1

    Re: Get count of files in subfolders too using File System Object (or some other way?


  6. #6
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,667

    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.
    Last edited by fafalone; Oct 9th, 2014 at 07:45 PM.

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    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

  8. #8
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Get count of files in subfolders too using File System Object (or some other way?

    Quote Originally Posted by DataMiser View Post
    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.

  9. #9
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    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.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  10. #10
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Get count of files in subfolders too using File System Object (or some other way?

    Quote Originally Posted by Nightwalker83 View Post
    I think baja_yu would have found an answer after 10 years.
    LOL.. I did not even notice the date of the OP

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