This uses RECURSION, and is FAST. The network permission problems might be slowing your down. Try MAPPING the drive first.
This will load all .JPG file names (in the \TEMP folder) into a listbox.
Code:
Option Explicit
' posted by Wokawidget
Private Sub cmdSearch_Click()
Dim colFiles As Collection
Dim objFile As File
Dim lngIndex As Long
Screen.MousePointer = vbHourglass
DoEvents
Set colFiles = New Collection
SearchFolders "D:\temp\", "*.jpg", True, colFiles
For lngIndex = 1 To colFiles.Count
Set objFile = colFiles.Item(lngIndex)
lstfiles.AddItem objFile.ParentFolder & "\" & objFile.Name
Next lngIndex
Screen.MousePointer = vbDefault
End Sub
Public Sub SearchFolders(ByVal pstrFolder As String, ByVal pstrFileSearch As String, ByVal pblnSearchSubFolders As Boolean, ByRef pcolFiles As Collection)
Dim objFolder As Folder
Dim objSubFolder As Folder
Dim objSubFolders As Folders
Dim objFile As File
Dim objFiles As Files
Dim objFSO As FileSystemObject
Set objFSO = New FileSystemObject
If objFSO.FolderExists(pstrFolder) Then
Set objFolder = objFSO.GetFolder(pstrFolder)
Set objFiles = objFolder.Files
For Each objFile In objFiles
If objFile.Name Like pstrFileSearch Then
pcolFiles.Add objFile
End If
Next objFile
Set objFiles = Nothing
If pblnSearchSubFolders Then
Set objSubFolders = objFolder.SubFolders
For Each objSubFolder In objSubFolders
SearchFolders objSubFolder.ParentFolder & "\" & objSubFolder.Name, pstrFileSearch, pblnSearchSubFolders, pcolFiles
Next objSubFolder
Set objSubFolders = Nothing
End If
Set objFolder = Nothing
End If
Set objFSO = Nothing
End Sub
Private Sub Command1_Click()
Dim ParentFolder As String, fld As Folder, fso As FileSystemObject
ParentFolder = "c:\Test"
Set fso = New FileSystemObject
Set fld = fso.GetFolder("c:\test")
RecursiveSearch fld, "*.*"
End Sub
Private Sub RecursiveSearch(Fold As Folder, FileName As String)
'will add all occurrences of FileName to a listbox List1
Dim fil As File, fld As Folder
For Each fil In Fold.Files
If fil.Name Like FileName Then List1.AddItem fil.Path
Next
For Each fld In Fold.SubFolders
RecursiveSearch fld, FileName
Next
End Sub
Yes. Mine would require a ListBox named List1
But you can change the line nto: If fil.Name Like FileName Then Debug.Print fil.Path
It depends onwhat you want to do with the found filename.
Yeah, datamiser has it... BTW: Wof, D, the fso is the slowest possible way to do a search, even the drive, dir, and file list boxes are faster (when they are not visible and just barely slower when they are only part of the time...)...
I kind of suspected that it may be a bit slow. I have never used it in any real VB code so could not be sure. I have used it in scripting and in embedded VB only then because the file methods I would normally use are not available.
I find out that if I want to have extremely fast routine then I must to have FindFirstFile and FindNextFile API solution or solution with Dir.
With the first one I can get 7400 files for 1.22 seconds. It is really fast!
Has anybody some suggestion about this to solution?
I find out that if I want to have extremely fast routine then I must to have FindFirstFile and FindNextFile API solution or solution with Dir.
With the first one I can get 7400 files for 1.22 seconds. It is really fast!
Has anybody some suggestion about this to solution?
Did you see the link I posted? It has lots of file API stuff
I'm aware that FSO is the slowest of available methods for that, but it allows for short and comprehensive code.
I once made this little program to compare API, VB Dir$ and FSO. It contains all three methods, where I took the FindFilesAPI() from allapi.net
You have to enter your own path in the txtPath textbox. It is hardcoded. It is only to compare speeds.
After clicking one of the buttons it takes a while to fill the listbox.
When done the bottom left textfield shows the elapsed time in milliseconds.
To measure the runtime I make use of a little dll (GetTime.dll) a friend of mine wrote for me in C++. It is not for distribution, please.
The program shows how API recursive walk through folders is done and should help understand and rewrite for own purposes.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.