Filesearch replacement

Domski

Well-known Member
Joined
Jan 18, 2005
Messages
7,292
I know I'm dragging up an old subject here but it kind of matters to me more now than it did when the it intially cropped up as we're moving onto 2010 soon from 2003 and I'm going to have to update my code.

I've seen various methods employed using DIR, FileSystemObject and Shell and being lazy I thought I'd see if anyone has a really good robust replacement they would recommend rather than trawling through dozens of threads on the subject.

Dom
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Sub Check_For_Specified_File_In_Directory_2007()
'only works in versions up to 2007 upwards
Dim myResult

myResult = fnt_FileOrDirExists("C:\")

End Sub

Function fnt_FileOrDirExists(PathName As String) As Boolean
Dim iTemp As Integer

'Ignore errors to allow for error evaluation
On Error Resume Next

iTemp = GetAttr(PathName)

'Check if error exists and set response appropriately
Select Case Err.Number

Case Is = 0

fnt_FileOrDirExists = True

Case Else

fnt_FileOrDirExists = False

End Select

'Resume error checking

On Error GoTo 0

End Function
 
Upvote 0
Thanks but not quite what I'm after.

Filesearch used to return an object (FoundFiles) containing the files returned and had an option to include sub directories. I could list dozens of threads on this forum and others that cover the subject but am after a kind of best practise function.

Quite why MS ditched this rather than fixing it still bemuses me.

Dom
 
Upvote 0
Here's one example but I'm sure it could be greatly improved:

Code:
Private Sub FileSearch(colFoundFiles As Collection, strPath As String, strMask As String, blnIncSubDir As Boolean)

Dim strDirFile As String
Dim varColItem As Variant
Dim colSubDir As New Collection


' Add backslash at the end of path if not present
strPath = Trim(strPath)
If Right(strPath, 1) <> "\" Then strPath = strPath & "\"


' Searching files accordant with mask
strDirFile = Dir(strPath & strMask)
Do While strDirFile <> ""
    colFoundFiles.Add strPath & strDirFile  'add file name to list(collection)
    strDirFile = Dir ' next file
Loop


' Procedure exiting if searching in subdirectories isn't enabled
If Not blnIncSubDir Then Exit Sub


' Searching for subdirectories in path
strDirFile = Dir(strPath & "*", vbDirectory)
Do While strDirFile <> ""
    ' Add subdirectory to local list(collection) of subdirectories in path
    If strDirFile <> "." And strDirFile <> ".." Then If ((GetAttr(strPath & strDirFile) And vbDirectory) = 16) Then colSubDir.Add strPath & strDirFile
    strDirFile = Dir 'next file
Loop


' Subdirectories list(collection) processing
For Each varColItem In colSubDir
     Call FileSearch(colFoundFiles, CStr(varColItem), strMask, blnIncSubDir) ' Recursive procedure call
Next


End Sub

Dom
 
Upvote 0

Forum statistics

Threads
1,225,625
Messages
6,186,071
Members
453,336
Latest member
Excelnoob223

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top