# Filesearch replacement



## Domski (Jul 25, 2012)

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


----------



## crazypabs (Jul 25, 2012)

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


----------



## Domski (Jul 25, 2012)

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


----------



## Domski (Jul 25, 2012)

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


```
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


----------



## Macropod (Jul 25, 2012)

See discussion at:
http://answers.microsoft.com/en-us/...lesearch/9021a162-7ec5-4c16-bff8-84f28300dba4

You can download the class file from: http://dl.dropbox.com/u/35239054/FileSearch.cls


----------



## Domski (Jul 25, 2012)

How have I missed that?

Think that pretty much covers it.

Thanks


----------

