Please tell me a good way to search a file in the hard drive given i know the file path. i want to search a file in a dirctory. for example, lets assume i have directory call "apps" and i want to search for file in this dirctory.
Printable View
Please tell me a good way to search a file in the hard drive given i know the file path. i want to search a file in a dirctory. for example, lets assume i have directory call "apps" and i want to search for file in this dirctory.
Use the Dir function.
Code:If Dir("C:\apps\myfile.exe") <> "" Then
Msgbox "file exists"
Else
Msgbox "file not found"
End If
'Put in form
If FileExists("c:\myapps\myfile.exe") Then
'file exists
else
'file does NOT exists
End if
'Put in module
Public Function FileExists(strPath) As Boolean
Dim fnum
fnum = FreeFile
FileExists = True
On Error GoTo err
Open strPath For Input As fnum
Close fnum
Exit Function
err:
Close fnum
FileExists = False
End Function
Dir function is much simpler and faster as well (I believe) :rolleyes:.
If you want it in a function:
Code:Private Function FileExists(file As String) As Boolean
If Dir(file) <> "" Then
FileExists = True
Else
FileExists = False
End If
End Function
Usage:
If FileExists("C:\apps\myfile.exe") Then
Msgbox "file exists"
Else
Msgbox "file not found"
End If
Yes, I used to use the DIR, but becomes useless when you're checking the existence of a file whilst already in a loop using DIR.
Solved many debug problems by dropping the DIR in favour of other methods.