Created: March 1, 1995
The Windows® function OpenFile provides a reliable method of determining whether or not a specific filename or path name exists.
The information in this article applies to:
The OpenFile function can be used to perform several file operations, such as creating a new file, deleting a file, or determining if a file exists. To declare this function within your program, include the following Declare statement in the Global Module or General Declarations section of a Visual Basic® for Windows® form:
Declare Function OpenFile% Lib "Kernel" (ByVal lpFileName$, lpReOpenBuff
As OFSTRUCT, ByVal wStyle%)
Note that this Declare statement must be typed as one single line of text.
The OpenFile function takes the following arguments, described as follows:
Argument | Description |
lpFileName | A string containing the name, which may or may not include a path name, to test. |
lpReOpenBuff | An OFSTRUCT structure that will contain information about the file after the OpenFile function has been called. |
wStyle | This combination of one or more flags specifies the type of operation that is to be performed on the file. |
In our demonstration program, we need only specify wStyle as the constant OF_EXIST.
After calling the OpenFile function, an integer value is returned. If a negative number is returned, the nErrCode value in the OFSTRUCT structure can be examined to find out if the file exists already.
The following program demonstrates how to determine if a file already exists.
'OFSTRUCT structure used by the OpenFile API function
Type OFSTRUCT '136 bytes in length
cBytes As String * 1
fFixedDisk As String * 1
nErrCode As Integer
reserved As String * 4
szPathName As String * 128
End Type
Declare Function OpenFile% Lib "Kernel" (ByVal lpFileName$, lpReOpenBuff As OFSTRUCT, ByVal wStyle%)
Dim wStyle As Integer
Dim Buffer As OFSTRUCT
Dim IsThere As Integer
Dim TestFile As String
Sub Form_Load()
TestFile = "c:\testfile.dat"
IsThere = OpenFile(TestFile, Buffer, OF_EXIST)
If IsThere < 0 Then
GoTo CheckForError
Else
Debug.Print "This file already exists"
End If
CheckForError:
IsThere = Buffer.nErrCode
If IsThere = 3 Then
Debug.Print "Pathname not found"
End If