How do you check to see if a file exists?
Printable View
How do you check to see if a file exists?
Use the Dir function.
Code:If Dir("C:\file.exe") <> "" Then
'file exists
Else
'file does not exist
End If
Thanks
Function FileExists(strPath As String) As Boolean
Dim lngRetVal As Long
On Error Resume Next
lngRetVal = Len(Dir$(strPath))
If Err Or lngRetVal = 0 Then
FileExists = False
Else
FileExists = True
End If
End Function
This way you can do something like this.
If FileExists("C:\temp\seenit.txt") = True Then
MsgBox "They Seen it!"
else
MsgBox "Didn't see it!"
End If
Or use the FileSystemObject.
Code:Dim X As FileSystemObject
If X.FileExists("MyFile.txt") Then 'File does exist