'*********************************************************
'* 名称: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

解决方案 »

  1.   

    '-----------------------------------------------------------
    ' &ordm;&macr;&Ecirc;&yacute;: FileExists
    ' &Aring;&ETH;&para;&Iuml;&Ecirc;&Ccedil;·&ntilde;&acute;&aelig;&Ocirc;&Uacute;&Ouml;&cedil;&para;¨&micro;&Auml;&Icirc;&Auml;&frac14;&thorn;
    '
    ' &Egrave;&euml;&iquest;&Uacute;: [strPathName] - &Ograve;&ordf;&frac14;ì&sup2;é&micro;&Auml;&Icirc;&Auml;&frac14;&thorn;
    '
    ' ·&micro;&raquo;&Oslash;: True&pound;&not;&Egrave;&ccedil;&sup1;&ucirc;&Icirc;&Auml;&frac14;&thorn;&acute;&aelig;&Ocirc;&Uacute;&pound;&raquo;·&ntilde;&Ocirc;ò&Icirc;&ordf; False
    '-----------------------------------------------------------
    '
    Function FileExists(ByVal strPathName As String) As Integer
        Dim intFileNum As Integer    On Error Resume Next    '
        ' &Egrave;&ccedil;&sup1;&ucirc;&Ograve;&yacute;&Oacute;&Atilde;&Aacute;&Euml;×&Ouml;·&ucirc;&acute;&reg;&pound;&not;&Eacute;&frac34;&sup3;&yacute;&Ograve;&yacute;&Oacute;&Atilde;
        '
    '    strPathName = strUnQuoteString(strPathName)
        '
        '&Eacute;&frac34;&sup3;&yacute;&Euml;ù&Oacute;&ETH;&Icirc;&sup2;&Euml;&aelig;&micro;&Auml;&Auml;&iquest;&Acirc;&frac14;·&Ouml;&cedil;&ocirc;·&ucirc;
        '
    '    If Right$(strPathName, 1) = gstrSEP_DIR Then
    '        strPathName = Left$(strPathName, Len(strPathName) - 1)
    '    End If    '
        '&Ecirc;&Ocirc;&Iacute;&frac14;&acute;ò&iquest;&ordf;&Icirc;&Auml;&frac14;&thorn;&pound;&not;±&frac34;&ordm;&macr;&Ecirc;&yacute;&micro;&Auml;·&micro;&raquo;&Oslash;&Ouml;&micro;&Icirc;&ordf; False
        '&Egrave;&ccedil;&sup1;&ucirc;&acute;ò&iquest;&ordf;&Ecirc;±&sup3;&ouml;&acute;í&pound;&not;·&ntilde;&Ocirc;ò&Icirc;&ordf; True
        '
        intFileNum = FreeFile
        Open strPathName For Input As intFileNum    FileExists = IIf(Err = 0, True, False)    Close intFileNum    Err = 0
    End Function
      

  2.   

    简单方法:
        dir()函数
      

  3.   

    dim YesNo as BooleanYesNo=dir "c:\11.dat"If YesNo =True Then 
       Msgbox "Ok"
    Else
       Msgbox "No"
    End id
      

  4.   

    用dir函数
    函数定义如下:
    Function Dir([PathName], [Attributes As VbFileAttribute = vbNormal]) As String
    如果文件存在,返回文件名,否则返回一个空值
    例如:
    Dim s As String
    s = Dir("c:\windows")
    If s = "" Then
      MsgBox "没有这个目录"
    End If
      

  5.   

    送你一个函数
    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
      

  6.   

    用dir函数。
    如:if dir("c:\abc.txt")<>"" then
        msgbox"文件存在"
        else
        msgbox"文件不存在"
        end if
      

  7.   

    还可以用FileSystemObjectdim objFSO as objectset objFSO=Createobject("Scripting.filesystemobject")msgbox objfso.FileExists("c:\abc.txt")set objfso=nothing
      

  8.   

    '*********************************************************
    '* 名称: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
      

  9.   

    if dir 路径\文件=""then
    不存在
    else
    存在
    end if
      

  10.   

    if dir(fileFileName)<>"" then
        MsgBox "文件已存!"
    else
        MsgBox "文件不存在!"
    end if
      

  11.   

    set file1 as createobject("Scripting.filesystemobject")
    if file1.f.fileexists("文件名") then
    msgbox "文件存在"
    else
    msgbox "文件不存在"
    end if