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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determining Whether a File Exists or Not

Folders and Files

Determining Whether a File Exists or Not

by  Swi  Posted    (Edited  )
This question has been asked many times in this forum. Here are 2 easy ways to determine whether a file exists or not:

' Usage for methods 1 and 2
Private Sub Command1_Click()
MsgBox FileExistsDIR("C:\Test.txt")
MsgBox FileExistsFSO("C:\Test.txt")
End Sub

' Method 1
Function FileExistsDIR(sFile As String) As Boolean
FileExistsDIR = True
If Dir$(sFile) = vbNullString Then FileExistsDIR = False
End Function

' Method 2
Function FileExistsFSO(sFile As String) As Boolean
Dim fs As Object
FileExistsFSO = True
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.FileExists(sFile) = False Then FileExistsFSO = False
Set fs = Nothing
End Function
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top