HOWTO: Determine If a File Exists by Using DIR$ID: Q112674
|
Visual Basic does not have any built-in functions that tell if a file exists or not. This article demonstrates how to find out if a file exists or not by using a Visual Basic program.
There are two different methods you can use to determine if a file exists:
Sub Command1_Click()
Dim TheFile as String
Dim Results as String
TheFile = "C:\AUTOEXEC.BAT"
Results = Dir$(TheFile)
If Results = "" Then
MsgBox "File Doesn't Exist!"
Else
MsgBox "File does Exist!"
End If
End Sub
If GetAttr(Path + DirName) And ATTR_DIRECTORY = ATTR_DIRECTORY Then
If (Count Mod 10) = 0 Then
ReDim Preserve D(Count + 10) ' Resize the array.
End If
Count = Count + 1 ' Increment counter.
D(Count) = DirName
End If
If GetAttr(path + DirName) = ATTR_DIRECTORY Then
If (Count Mod 10) = 0 Then
ReDim Preserve D(Count + 10) ' Resize the array.
End If
Count = Count + 1 ' Increment counter.
D(Count) = DirName
End If
For more information, see the Programmer's Reference, File Manipulation and the DIR/DIR$ commands.
Additional query words: existence
Keywords : kbprg kbVBp400 kbVBp500 kbhowto PrgOther VB4WIN
Version : 3.0 4.0 5.0
Platform : NT WINDOWS
Issue type : kbhowto
Last Reviewed: June 1, 1999