Private Sub Command1_Click() Dim myfile As String myfile = Dir("c:\1new.mdb") MsgBox myfile End Sub看msdn的dir函数
或者fsoOption Explicit Dim fs As New FileSystemObjectPrivate Sub Command1_Click() If fs.FileExists("c:\new.mdb") Then MsgBox "存在" Else MsgBox "不存在" End If End Sub
'检测文件sFilename是否存在 Public Function FileExists(sFilename As String) As Boolean On Error Resume Next FileLen sFilename FileExists = (Err.Number = 0) End Function
if Dir("c:\test.txt")="" then msgbox "不存在" else msgbox "存在" end if
if Dir(PathName)="" then msgbox "不存在" else msgbox "存在" end if
dir(PathName)不行! PathName是进行模糊匹配 换言之你找C:\test.txt 如果没有C:\test.txt有个C:\te.txt它也认为找到啦!我在我遍的一个软件中是用: On Error Resume Next Dim X As Integer X = FreeFile Open SetFilePath For Input As X If Err.Number = 0 Then MsgBox "存在" Else msgbox "不存在" End If
to broown:你再试试,好像没有你说的模糊匹配。dir没问题。
要是用API Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long也可以不过简单普遍的可以楼上的
Option Explicit Dim fs As New FileSystemObjectPrivate Sub Command1_Click() If fs.FileExists("c:\new.mdb") Then MsgBox "存在" Else MsgBox "不存在" End If End Sub
Dim myfile As String
myfile = Dir("c:\1new.mdb")
MsgBox myfile
End Sub看msdn的dir函数
Dim fs As New FileSystemObjectPrivate Sub Command1_Click()
If fs.FileExists("c:\new.mdb") Then
MsgBox "存在"
Else
MsgBox "不存在"
End If
End Sub
Public Function FileExists(sFilename As String) As Boolean
On Error Resume Next
FileLen sFilename
FileExists = (Err.Number = 0)
End Function
MSDN上有详细的例子,一看就知道了,你输入filesystemobject
就可以看到了
msgbox "不存在"
else
msgbox "存在"
end if
msgbox "不存在"
else
msgbox "存在"
end if
PathName是进行模糊匹配
换言之你找C:\test.txt
如果没有C:\test.txt有个C:\te.txt它也认为找到啦!我在我遍的一个软件中是用:
On Error Resume Next
Dim X As Integer
X = FreeFile
Open SetFilePath For Input As X
If Err.Number = 0 Then
MsgBox "存在"
Else
msgbox "不存在"
End If
Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long也可以不过简单普遍的可以楼上的
open一下你所指定的文件名,如果出现错误了说明不存在,如果成功说明存在。当然在做这个动作之前要on error goto 一下
Dim fs As New FileSystemObjectPrivate Sub Command1_Click()
If fs.FileExists("c:\new.mdb") Then
MsgBox "存在"
Else
MsgBox "不存在"
End If
End Sub
一定要在工程里引用,Microsoft Scripting Runtime
我最开始就是用dir的,但,我发现当我改名时(如:test.txt --> tesst.txt)重新执行程序,程序提示能找到,但我一旦真正要操作这个文件时,程序就出现了错误(显然是,并没有找到这个文件)
要操作这个文件时用dir
我用
If Dir("c:\test.txt") = "" Then
MsgBox "不存在"
Else
MsgBox "存在"
End If
是可以的呀。。
如果把test.txt-->tesst.txt时就找不到了呀!!!!