|
-
Nov 13th, 2000, 09:32 AM
#1
Thread Starter
Junior Member
I would like to be able to search an entire drive for a particular type of file. I know I can use the DIR function to search within a specific directory but is there a way to start at the root and search all the directories?
-
Nov 13th, 2000, 09:40 AM
#2
transcendental analytic
How about this?
Code:
Option Explicit
'API Consts, Types and Functions
Private Const MAX_PATH = 260
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 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 GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private aFileList() as string
Private nFileCount As Long
Function FastFindFiles(ByVal sFolder As String, Optional ByVal sPattern As String = "*") As Variant
'Initialize the Private File Array and Count, then call the Fast
'File Recursive Function to populate the Array, then return it.
nFileCount = 0
aFileList = Array()
Screen.MousePointer = vbArrowHourglass
Call RecurseFindFiles(sFolder, sPattern)
Screen.MousePointer = vbDefault
FastFindFiles = aFileList
End Function
Private Sub RecurseFindFiles(ByVal sFolder As String, ByVal sPattern As String)
Dim tFD As WIN32_FIND_DATA, lFile As Long, bFound As Long, aSubs() As String, nSubs As Long, sFilename As String
'Make sure the passed folder includes an ending "\"
If Right(sFolder, 1) <> "\" Then sFolder = sFolder & "\"
'Find the First File in the Specified Location
lFile = FindFirstFile(sFolder & "*", tFD)
bFound = lFile
'Loop while a File is found
While bFound
'Get the Filename
sFilename = UCase(Left(tFD.cFileName, InStr(tFD.cFileName, Chr(0)) - 1))
If (tFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) Then
'If it's a Folder, add it to the Sub Folders Array
If Left(sFilename, 1) <> "." Then
ReDim Preserve aSubs(nSubs)
aSubs(nSubs) = sFilename
nSubs = nSubs + 1
End If
Else
'If it's a File, compare it to the Pattern for a Match
If sFilename Like UCase(sPattern) Then
'If it matches, add it to the File Array
ReDim Preserve aFileList(nFileCount)
aFileList(nFileCount) = sFolder & sFilename
nFileCount = nFileCount + 1
End If
End If
'Find the Next File, (if there is one).
bFound = FindNextFile(lFile, tFD)
Wend
'Close the API Find Handle
Call FindClose(lFile)
'If there were Sub Folders found, Recurse them too..
If nSubs Then
For nSubs = 0 To UBound(aSubs)
Call RecurseFindFiles(sFolder & aSubs(nSubs), sPattern)
Next
End If
End Sub
'Usage:
Dim Files() as string
Files = FastFindFiles ("C:\","Myfile.exe")
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Nov 13th, 2000, 09:41 AM
#3
Fanatic Member
Check out this thread:
http://forums.vb-world.net/showthrea...threadid=34052
I used the FileSystemObject to start at a base directory then recurse through it's sub-directories. It might suit your purposes.
-
Dec 2nd, 2000, 11:32 AM
#4
Addicted Member
When running this code, I get the following error:
aFileList = Array() <--- Run time error '13'
Type Mismatch
anyone have an idea why ?
-
Dec 2nd, 2000, 03:35 PM
#5
transcendental analytic
Sorry about that, the changes were made from a vb5 version of it, back to vb6, and i didn't have time to look over it. Remove these:
Code:
Function FastFindFiles(ByVal sFolder As String, Optional ByVal sPattern As String = "*") As Variant As String()'put this instead
'Initialize the Private File Array and Count, then call the Fast
'File Recursive Function to populate the Array, then return it.
nFileCount = 0
aFileList = Array()
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Dec 2nd, 2000, 04:14 PM
#6
Addicted Member
Thank you very much. Works much better !
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|