function fileexsit(filename as string) as boolean if dir(filename)<>"" then fileexsit=true else fileexsit=false end function
dim FileStr as string FileStr=Dir("C:\config.sys") if FileStr="" then msgbox "文件不存在!!!" else msgbox "文件存在!!!" end if
Dim fs As New FileSystemObject while fs.FileExists(text1.text)'text1.text为完整文件名 MsgBox "文件已经存在", vbOKOnly , "操作提示" wend另需要在project - references中选中microsoft scripting runtime前面的框
用dir函数最简单了,msdn中有好多例子的: Dim MyFile, MyPath, MyName' 返回“WIN.INI” (如果该文件存在)。 MyFile = Dir("C:\WINDOWS\WIN.ini") ' 返回带指定扩展名的文件名。如果超过一个 *.ini 文件存在, ' 函数将返回按条件第一个找到的文件名。 MyFile = Dir("C:\WINDOWS\*.ini")' 若第二次调用 Dir 函数,但不带任何参数,则函数将返回同一目录下的下一个 *.ini 文件。 MyFile = Dir' 返回找到的第一个隐式 *.TXT 文件。 MyFile = Dir("*.TXT", vbHidden)' 显示 C:\ 目录下的名称。 MyPath = "c:\" ' 指定路径。 MyName = Dir(MyPath, vbDirectory) ' 找寻第一项。 Do While MyName <> "" ' 开始循环。 ' 跳过当前的目录及上层目录。 If MyName <> "." And MyName <> ".." Then ' 使用位比较来确定 MyName 代表一目录。 If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then Debug.Print MyName ' 如果它是一个目录,将其名称显示出来。 End If End If MyName = Dir ' 查找下一个目录。 Loop
Public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long Public Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long Public Const INVALID_HANDLE_VALUE = -1 Public Const MAX_PATH = 260 Public Type FILETIME dwLowDateTime As Long dwHighDateTime As Long End Type Public Type WIN32_FIND_DATA dwFileAttributes As Long ftCreationTime As FILETIME ftLastAccessTime As FILETIME ftLastWriteTime As FILETIME nFileSizeHigh As Long nFileSizeLow As Long dwReserved0 As Long dwReserved1 As Long cFileName As String * MAX_PATH cAlternate As String * 14 End Type Public Function findFile(filename As String) As Boolean Dim hff As Long Dim X As WIN32_FIND_DATA hff = FindFirstFile(filename, X) If hff = INVALID_HANDLE_VALUE Then findFile = False Else findFile = True End If hff = FindClose(hff) End Function
还可以 function fileexsit(filename as string) as boolean on error goto 12 open filename as output as #1 fileexsit=ture exit function 12 fileexsit=false end function
Function FileExsit(FileName As String) As Boolean On Error GoTo 12 Open FileName For Input As #1 '如果用Output 那么文件就会被清空了(完了!) Close #1 FileExsit = Truev'''' Exit Function 12 FileExsit = False End Function '2个土方法和一个正规的 Private Sub Command1_Click() On Error GoTo 100 Name "C:\config.sys" As "C:\config.sys11" Name "C:\config.sys11" As "C:\config.sys" MsgBox "文件存在!!!" Exit Sub 100 MsgBox "文件不存在!!!" End SubPrivate Sub Command2_Click() On Error GoTo 200 Open "C:\config.sys" For Input As #1 Close #1 MsgBox "文件存在!!!" Exit Sub 200 MsgBox "文件不存在!!!" End Sub Private Sub Command3_Click() Dim FileStr As String FileStr = Dir("C:\config.sys") If FileStr = "" Then MsgBox "文件不存在!!!!!!!!!" Else MsgBox "文件存在!" End If End Sub
'错了 Function FileExsit(FileName As String) As Boolean On Error GoTo 100 Open FileName For Input As #1 Close #1 FileExsit = True Exit Function 100 FileExsit = False End Function
if dir(filename)<>"" then fileexsit=true else fileexsit=false
end function
FileStr=Dir("C:\config.sys")
if FileStr="" then
msgbox "文件不存在!!!"
else
msgbox "文件存在!!!"
end if
MsgBox "文件已经存在", vbOKOnly , "操作提示"
wend另需要在project - references中选中microsoft scripting runtime前面的框
Dim MyFile, MyPath, MyName' 返回“WIN.INI” (如果该文件存在)。
MyFile = Dir("C:\WINDOWS\WIN.ini") ' 返回带指定扩展名的文件名。如果超过一个 *.ini 文件存在,
' 函数将返回按条件第一个找到的文件名。
MyFile = Dir("C:\WINDOWS\*.ini")' 若第二次调用 Dir 函数,但不带任何参数,则函数将返回同一目录下的下一个 *.ini 文件。
MyFile = Dir' 返回找到的第一个隐式 *.TXT 文件。
MyFile = Dir("*.TXT", vbHidden)' 显示 C:\ 目录下的名称。
MyPath = "c:\" ' 指定路径。
MyName = Dir(MyPath, vbDirectory) ' 找寻第一项。
Do While MyName <> "" ' 开始循环。
' 跳过当前的目录及上层目录。
If MyName <> "." And MyName <> ".." Then
' 使用位比较来确定 MyName 代表一目录。
If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
Debug.Print MyName ' 如果它是一个目录,将其名称显示出来。
End If
End If
MyName = Dir ' 查找下一个目录。
Loop
Public Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Public Const INVALID_HANDLE_VALUE = -1
Public Const MAX_PATH = 260
Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Public Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
Public Function findFile(filename As String) As Boolean
Dim hff As Long
Dim X As WIN32_FIND_DATA
hff = FindFirstFile(filename, X)
If hff = INVALID_HANDLE_VALUE Then
findFile = False
Else
findFile = True
End If
hff = FindClose(hff)
End Function
function fileexsit(filename as string) as boolean
on error goto 12
open filename as output as #1
fileexsit=ture
exit function
12
fileexsit=false
end function
On Error GoTo 12
Open FileName For Input As #1 '如果用Output 那么文件就会被清空了(完了!)
Close #1
FileExsit = Truev''''
Exit Function
12
FileExsit = False
End Function
'2个土方法和一个正规的
Private Sub Command1_Click()
On Error GoTo 100
Name "C:\config.sys" As "C:\config.sys11"
Name "C:\config.sys11" As "C:\config.sys"
MsgBox "文件存在!!!"
Exit Sub
100
MsgBox "文件不存在!!!"
End SubPrivate Sub Command2_Click()
On Error GoTo 200
Open "C:\config.sys" For Input As #1
Close #1
MsgBox "文件存在!!!"
Exit Sub
200
MsgBox "文件不存在!!!"
End Sub
Private Sub Command3_Click()
Dim FileStr As String
FileStr = Dir("C:\config.sys")
If FileStr = "" Then
MsgBox "文件不存在!!!!!!!!!"
Else
MsgBox "文件存在!"
End If
End Sub
Function FileExsit(FileName As String) As Boolean
On Error GoTo 100
Open FileName For Input As #1
Close #1
FileExsit = True
Exit Function
100
FileExsit = False
End Function