Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search for a file on all drives, including network drives 1

Status
Not open for further replies.

scousethemoose

Programmer
Jul 14, 2002
69
AU
Hi I'm trying to write a simple routine to search for an Access Database backend located on a network server.

This routine is run everytime my database frontend is opened on a clients machine. At the moment I'm using a very crude way using the file system object, checking to see if a driveExists(A-Z) then checking if a the file exists using the fileExists()function.

The problem with this is that I have to specify a path for the file this may change ofcourse.

Ideally I want a routine which just searches all drives for the file then if found open the frontend else close. I've looked at some recursive search examples they seem to be overcomplicated and quite confusing.

Can any one point me in the right direction? :)
 

Search this site for any one of the following

Dir
File System Object
FindFirst

Good Luck

 
>I've looked at some recursive search examples they seem to be overcomplicated and quite confusing

Well, you'll be pleased to know that there are quite a number of recursive solutions to your problem (or close relatives) in this forum, most of which are not complicated/confusing :)
 
Hi thanks to everyone for pointing me in the right direction, I've played around with code I've found and come up with this. It seems to work fine, but can anyone see any problems with it.

Code:
Option Explicit
Dim fso As New FileSystemObject
Dim fld As Folder

Private Sub Form_Load()
Dim x As Long
Dim validDrive As Boolean
Dim fileExists As Boolean
Dim fileName As String
Dim nFiles As Long

' Database Backend
fileName = "APManager.apm"

x = 65

' Simple search for database backend using file system object
    Do While fileExists = False Or x = 91
        x = x + 1
        
        validDrive = fso.DriveExists(Chr$(x)) '(A - Z) including network drives I hope
            
        ' once a valid drive is found then check for the file
        If validDrive = True Then
        
        ' Check if file exists by calling recursive file search routine
            ' MsgBox validDrive & " " & Chr$(x)
            fileExists = FindFile(Chr$(x) & ":\", fileName, nFiles)
        End If
    Loop
    
    ' Debug.Print x
    ' Debug.Print fileExists
    
End Sub

Private Function FindFile(ByVal sFol As String, sFile As String, nFiles As Long) As Boolean

Dim tFld As Folder, tFil As File, fileName As String
   
   On Error GoTo cancelSearch
   Set fld = fso.GetFolder(sFol)
   fileName = Dir(fso.BuildPath(fld.Path, sFile), vbNormal Or vbHidden Or vbSystem Or vbReadOnly)
   
   ' Setup base directory and begin search
   While Len(fileName) <> 0
      FindFile = FindFile + FileLen(fso.BuildPath(fld.Path, fileName))
      nFiles = nFiles + 1
      
      If nFiles = 1 Then
        MsgBox &quot;File Found: &quot; & fileName, vbInformation, App.Title
        Exit Function
      Else
        fileName = Dir()  ' Go to the next file
        DoEvents
      End If
   Wend
   
   ' Search sub folders for file
   If fld.SubFolders.Count > 0 Then
      For Each tFld In fld.SubFolders
         DoEvents
         FindFile = FindFile + FindFile(tFld.Path, sFile, nFiles)

      Next
   End If
   
   Exit Function
   
cancelSearch:
fileName = &quot;&quot;
Resume Next

End Function

Any further advice would be greatly appreciated :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top