'*********************************************************
'* 名称:FileExists%(filename$)
'* 功能:此函数用于判断文件是否存在
'* 用法:FileExists%('文件名称')
'* 文件不存在返回 0
'*********************************************************
Function FileExists%(filename$)
Dim F%
On Error Resume Next
F% = FreeFile
Open filename$ For Input As #F%
Close #F%
FileExists% = Not (Err <> 0)
End Function
'* 名称:FileExists%(filename$)
'* 功能:此函数用于判断文件是否存在
'* 用法:FileExists%('文件名称')
'* 文件不存在返回 0
'*********************************************************
Function FileExists%(filename$)
Dim F%
On Error Resume Next
F% = FreeFile
Open filename$ For Input As #F%
Close #F%
FileExists% = Not (Err <> 0)
End Function
' º¯Êý: FileExists
' ÅжÏÊÇ·ñ´æÔÚÖ¸¶¨µÄÎļþ
'
' Èë¿Ú: [strPathName] - Òª¼ì²éµÄÎļþ
'
' ·µ»Ø: True£¬Èç¹ûÎļþ´æÔÚ£»·ñÔòΪ False
'-----------------------------------------------------------
'
Function FileExists(ByVal strPathName As String) As Integer
Dim intFileNum As Integer On Error Resume Next '
' Èç¹ûÒýÓÃÁË×Ö·û´®£¬É¾³ýÒýÓÃ
'
' strPathName = strUnQuoteString(strPathName)
'
'ɾ³ýËùÓÐβËæµÄĿ¼·Ö¸ô·û
'
' If Right$(strPathName, 1) = gstrSEP_DIR Then
' strPathName = Left$(strPathName, Len(strPathName) - 1)
' End If '
'ÊÔͼ´ò¿ªÎļþ£¬±¾º¯ÊýµÄ·µ»ØֵΪ False
'Èç¹û´ò¿ªÊ±³ö´í£¬·ñÔòΪ True
'
intFileNum = FreeFile
Open strPathName For Input As intFileNum FileExists = IIf(Err = 0, True, False) Close intFileNum Err = 0
End Function
dir()函数
Msgbox "Ok"
Else
Msgbox "No"
End id
函数定义如下:
Function Dir([PathName], [Attributes As VbFileAttribute = vbNormal]) As String
如果文件存在,返回文件名,否则返回一个空值
例如:
Dim s As String
s = Dir("c:\windows")
If s = "" Then
MsgBox "没有这个目录"
End If
Function FileExists%(FileName$) '判断固定文件是否存在
' Description
' Checks 'filename$' to find wether the filename given
' exists.
'
' Parameters
' Name Type Value
' -------------------------------------------------------------
' filename$ String The filename to be checked
'
' Returns
' True if the file exists
' False if the file does not exist
'
Dim F%
' Trap any errors that may occur
On Error Resume Next
' Get a free file handle to avoid using a file handle already in use
F% = FreeFile
' Open the file for reading
Open FileName$ For Input As #F%
' Close it
Close #F%
' If there was an error, Err will be <> 0. In that case, we return False
FileExists% = Not (Err <> 0)
End Function
如:if dir("c:\abc.txt")<>"" then
msgbox"文件存在"
else
msgbox"文件不存在"
end if
'* 名称:FileExists%(filename$)
'* 功能:此函数用于判断文件是否存在
'* 用法:FileExists%('文件名称')
'* 文件不存在返回 0
'*********************************************************
Function FileExists%(filename$)
Dim F%
On Error Resume Next
F% = FreeFile
Open filename$ For Input As #F%
Close #F%
FileExists% = Not (Err <> 0)
End Function
不存在
else
存在
end if
MsgBox "文件已存!"
else
MsgBox "文件不存在!"
end if
if file1.f.fileexists("文件名") then
msgbox "文件存在"
else
msgbox "文件不存在"
end if