'The file you want to check
Const strFile = "C:\testfile.txt"Private Sub Form_Load()
Dim msg As String
If ReportFileStatus(strFile) Then
msg = strFile & " exists."
Else
msg = strFile & " doesn't exist."
End If
MsgBox msg
End Sub
'Add the Reference of Microsoft Scripting Runtime
Function ReportFileStatus(filespec As String) As Boolean Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists(filespec)) Then
ReportFileStatus = True
Else
ReportFileStatus = False
End If
End FunctionFunction ReportFileStatus(filespec As String) As Boolean
Dim strdir As String
strdir = Dir(filespec)
If strdir <> "" Then
ReportFileStatus = True
Else
ReportFileStatus = False
End If
End Function
Const strFile = "C:\testfile.txt"Private Sub Form_Load()
Dim msg As String
If ReportFileStatus(strFile) Then
msg = strFile & " exists."
Else
msg = strFile & " doesn't exist."
End If
MsgBox msg
End Sub
'Add the Reference of Microsoft Scripting Runtime
Function ReportFileStatus(filespec As String) As Boolean Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists(filespec)) Then
ReportFileStatus = True
Else
ReportFileStatus = False
End If
End FunctionFunction ReportFileStatus(filespec As String) As Boolean
Dim strdir As String
strdir = Dir(filespec)
If strdir <> "" Then
ReportFileStatus = True
Else
ReportFileStatus = False
End If
End Function
object 必需的。始终是一个 FileSystemObject 的名字。
filespec 必需的。要确定是否存在的文件的名字。如果认为文件不在当前文件夹中,必须提供一个完整的路径说明(绝对的或相对的)。
Public Function FileIsExist(Filename As String, Optional Attribus As VbFileAttribute = vbArchive) As Boolean
Dim cName As String
cName = Dir(Filename, Attribus)
FileIsExist = (Format(cName, ">") = Format(StripFile(Filename), ">"))
End FunctionPublic Function StripFile(Name As String) As String
Dim pos As Integer, pos1 As Integer
pos = InStr(Name, "\")
While pos <> 0
If pos <> 0 Then pos1 = pos
pos = InStr(pos + 1, Name, "\")
Wend
StripFile = Right(Name, Len(Name) - pos1)
End Function