Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2011
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2.  
  3. Private Const INVALID_HANDLE_VALUE As Long = -1
  4. Private Const MAX_PATH As Long = 260
  5. Private Const FILE_ATTRIBUTE_DIRECTORY As Long = &H10
  6.  
  7. Private Type FILETIME
  8.     dwLowDateTime As Long
  9.     dwHighDateTime As Long
  10. End Type
  11.  
  12. Private Type WIN32_FIND_DATA
  13.     dwFileAttributes As Long
  14.     ftCreationTime As FILETIME
  15.     ftLastAccessTime As FILETIME
  16.     ftLastWriteTime As FILETIME
  17.     nFileSizeHigh As Long
  18.     nFileSizeLow As Long
  19.     dwReserved0 As Long
  20.     dwReserved1 As Long
  21.     cFileName As String * MAX_PATH
  22.     cAlternate As String * 14
  23. End Type
  24.  
  25. Private Declare Function FindFirstFile Lib "kernel32.dll" Alias "FindFirstFileA" (ByVal lpFileName As String, ByRef lpFindFileData As WIN32_FIND_DATA) As Long
  26. Private Declare Function FindNextFile Lib "kernel32.dll" Alias "FindNextFileA" (ByVal hFindFile As Long, ByRef lpFindFileData As WIN32_FIND_DATA) As Long
  27. Private Declare Function FindClose Lib "kernel32.dll" (ByVal hFindFile As Long) As Long
  28.  
  29. Private Sub ObjectFound(IsDirectory As Boolean, Name As String)
  30.     If IsDirectory Then Name = "[" & Name & "]"
  31.     MsgBox Name
  32. End Sub
  33.  
  34. Private Function GoEnum(Path As String, Recursive As Boolean) As Boolean
  35. 'This function returns TRUE if it has failed, otherwise FALSE.
  36. Dim fd As WIN32_FIND_DATA, IsDir As Boolean
  37.  Dim hFind As Long, Name As String, Res As Boolean
  38.  hFind = FindFirstFile(Path & "*.*", fd)
  39.  If hFind <> INVALID_HANDLE_VALUE Then
  40.   Res = True
  41.   Do
  42.    Name = Left$(fd.cFileName, InStr(1, fd.cFileName, vbNullChar) - 1)
  43.    If Len(Name) > 0 And Name <> "." And Name <> ".." Then
  44.     If (fd.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = 0 Then
  45.      Call ObjectFound(False, Path & Name)
  46.     Else
  47.      Name = Name & "\"
  48.      Call ObjectFound(True, Path & Name)
  49.      If Recursive Then Res = Res And Not (GoEnum(Path & Name, True))
  50.     End If
  51.    End If
  52.   Loop While FindNextFile(hFind, fd) <> 0
  53.   FindClose hFind
  54.   GoEnum = Res
  55.  Else
  56.   GoEnum = True
  57.  End If
  58. End Function
  59.  
  60. Public Function Enumerate(ByVal Path As String, Optional Recursive As Boolean = False) As Boolean
  61.  Path = Replace$(Path, "/", "\")
  62.  If Right$(Path, 1) <> "\" Then Path = Path & "\"
  63.  Enumerate = GoEnum(Path, Recursive)
  64. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement