ok, befor I open a file, I want to make sure the path, and the file exist. How do I do that?
Printable View
ok, befor I open a file, I want to make sure the path, and the file exist. How do I do that?
Code:if len(Dir$(path)) <> 0 Then "File Exists"
Try:
if len(dir$(path))=0 then 'file doesn't exist
That was weird, almost the exact same syntax...:D
Yup, this has got to be one of the most common questions hey Lethal?
yeah, it's in the top ten..:D
You just have to be a bit careful as the Dir function will give you an error if you try and access an invalid drive or drive without a disk (eg A Drive)
For example:
Dir("A:\test.txt") will give an error if there is no disk in the A drive. The following function takes care of this.
Most of the time, this a complete waste of energy but this has pissed me off in the past. :)Code:
Public Function FileExists(FileName As String) As Boolean
On Error GoTo FileNotFound
If Len(Dir$(FileName)) <> 0 Then
FileExists = True
Else
FileExists = False
End If
Exit Function
FileNotFound:
FileExists = False
End Function
Hope this helps,
Nathan Liebke
Or simply use the On Error Resume Next or another inline error handler routine. I was presumming he was going to do some validation before porting it to "Live Data".
Yep, that would work too.:)