i need a function that returns true or false, wether a file exists or not
Printable View
i need a function that returns true or false, wether a file exists or not
Here you go... Add a textbox and a commandbutton to a form to test this example:
Code:Private Sub Command1_Click()
If Dir(Text1.Text) <> "" Then
MsgBox "File """ & Text1.Text & """ found!"
Else
MsgBox "File """ & Text1.Text & """ not found!"
End If
End Sub
You can use the following code as a function
Function File_Exist(FileName As String) As Integer
Dim Cp As Integer
On Local Error Resume Next
Cp = FreeFile
Open FileName For Input As Cp
File_Exist = IIf(Err, True, False)
Close Cp
Err = 0
End Function
Juz use Dir function is enough.
Code:Public Function fileexists(strfilepathandname As String) As Boolean
On Error Resume Next
Dim lngcheck As Long
If InStr(strfilepathandname$, ".") = 0& Then
Let fileexists = False
End If
Let lngcheck& = Len(Dir$(strfilepathandname$))
If lngcheck& = 0& Then
Let fileexists = False
Exit Function
Else
Let fileexists = True
End If
Exit Function
End Function
'Usage:
'If fileexists("C:\Autoexec.bat") Then
'Msgbox "file exists."
'Else
'Msgbox "file does not exist."
'End If
Code:'Declaration
Set fso = CreateObject("Scripting.FileSystemObject")
Syntax: object.fileexists (filespec)
'Example:
If fso.FileExists("C:\Windows\win.com") Then
'Code if exists
Else
'Code if not
End If