
Check to see if a file exits...
There are a couple of ways to check if a file exists.....
1. Use DIR$
Dim sFile$
sFile = "C:\xyz.txt"
If Dir$(sFile, vbNormal) <> "" Then ' the file exists
2. Use FileLength() --- requires error handling
Dim sFile$, dwLength&
sFile = "C:\xyz.txt"
On Error Resume Next
dwLength = FileLength(sFile)
If Err.Number = 0 Then ' the file exists
3. Use OpenFile() API call --- more sophisticated, but works really well
' in global area
Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer ' or Long for Win32
rsvd As Long
szPathName As String * 128
End Type
Public Const OF_EXIST = &H4000
Public Const HFILE_ERROR = -1
Declare Function OpenFile% Lib "KERNEL" (ByVal lpszFile As Any, ofs As
OFSTRUCT, ByVal uFlags%)
' in routine
sFile = "C:\xyz.txt"
Dim ofs As OFSTRUCT
With ofs
.cBytes = Len(ofs)
.fFixedDist = 0
.nErrCode = 0
.rsvd = 0
.szPathName = String$(128, Chr$(0))
End With
If (OpenFile(ByVal sFile, ofs, OF_EXIST) <> HFILE_ERROR) Then ' the file
exists
Normally, I use the API routine. I have a routine that calls it that I use
in all my apps. This method is nice because it will never generate a
run-time error if the file doesn't exist. You only need to check the return
value.
Hope this helps,
W. Clifford
Quote:
> Does anyone know the function that you call to see if a file exists?
> I used to know the function but I forgot and can't bring up anything in
the
> help.
> Any help would be appreciated.
> Thanx