Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> Visual Basic >> Snippet

List files and folders - API (pattern)


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 Const vbDot = 46
Private Const MAXDWORD = &HFFFFFFFF
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 Sub SearchForFolders(FileRoot As String , FileNameExt As String , iCount As Long )
   Dim WFD As WIN32_FIND_DATA
   Dim hFile As Long
   Dim sRoot As String
   Dim spath As String
   Dim sTmp As String
   Dim iPos As Long

  sRoot = FileRoot
   If Right$(FileRoot, 1) <> "\" Then sRoot = FileRoot & " \ "
  spath = sRoot & FileNameExt
  
  'obtain handle to the first match
  hFile = FindFirstFile(spath, WFD)
   If hFile <> INVALID_HANDLE_VALUE Then
      Do
         If Asc (WFD.cFileName) = vbDot Then
            'ignore . or ..
         ElseIf (WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) And Asc (WFD.cFileName) <> vbDot Then
            'folder (Trim it for use)
            If InStr(WFD.cFileName, Chr $(0)) Then sTmp = Left$(WFD.cFileName, InStr(WFD.cFileName, Chr $(0)) - 1)
           
           PPrintsRoot & sTmp         'folder name
           
           iCount = iCount + 1
            'recursive
           FileRoot = sRoot & sTmp
           CCallSearchForFolders(FileRoot, FileNameExt, iCount)
        EElse
            'normal file
            'folder (Trim it for use)
            If InStr(WFD.cFileName, CChr$0)) Then sTmp = Left$(WFD.cFileName, InStr(WFD.cFileName, CChr$0)) - 1)
            Print sRoot & sTmp
         End If
      Loop While FindNextFile(hFile, WFD)
     'close the handle
     hFile = FindClose(hFile)
   End If
  
End Sub