引用Microsoft Scripting RuntimeDim fs As FileSystemObject
    Dim sFile As String
    
    sFile = "E:\12.txt"
    Set fs = New FileSystemObject
    
    If fs.FileExists(sFile) Then
        MsgBox "文件存在"
    Else
        MsgBox "文件不存在"
    End If

解决方案 »

  1.   

    最好用FileSystemObject 对象,它能对文件进行检查、更名、Copy ...
    或打开文件,查看它是否出错!
      

  2.   

    --------------------------------
    使用API是最好的方法:(最准确)
    --------------------------------
    呵呵,我写了如下代码,你可以拿去使用Private Const MAX_PATH As Long = 260
    Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
    Private Const INVALID_HANDLE_VALUE = -1Private Type FILETIME
            dwLowDateTime As Long
            dwHighDateTime As Long
    End TypePrivate 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 TypePublic Function DoesFileExistEx(ByVal sFile As String) As Boolean
        Dim FindData As WIN32_FIND_DATA
        Dim GetFile As Long
        GetFile = FindFirstFile(sFile, FindData)
        If GetFile <> INVALID_HANDLE_VALUE Then
            If (FindData.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) Then
                DoesFileExistEx = False 'Is Dir
            Else
                DoesFileExistEx = True
            End If
        Else
            DoesFileExistEx = False
        End If
        FindClose GetFile
    End Function
    --------------------------------
    '使用示例:
    MsgBox DoesFileExistEx("c:\1.txt")
      

  3.   

    你可以使用Api函数CreateFile():hOutFile = CreateFile (TEXT("aaa.TXT"), // Open aaa.TXT
                    GENERIC_READ|GENERIC_WRITE,      // Open for readingand write
                FILE_SHARE_READ|FILE_SHARE_WRITE,     // Share for reading and write
                          NULL, // No security
                     CREATE_ALWAYS, // If ont file,create it
                         FILE_FLAG_RANDOM_ACCESS, //Random access file
                          NULL); // No template file if (hOutFile == INVALID_HANDLE_VALUE)  // 文件不存在
    MessageBox("不能打开aaa.txt文件");CREATE_ALWAYS的作用就是如果文件存在,就覆盖掉文件内容。
    hOutFile 是文件句柄,返回值是INVALID_HANDLE_VALUE表示文件不存在。
    具体的你可以看一下CreateFile()函数。
      

  4.   

    对不起,分散不出去,偶还有个问题想问问,就是如何创建一个BAT文件?
      

  5.   

    BAT文件只是一个文本文件而已你可以使用文件操作中的
    Print #x,str   'x为文件号呵呵,文件操作很简单,自己去写啦
      

  6.   

    CreateFile()函数本身就可以创建文件呀。
    它可以追加,覆盖,新建,等等。同时它也可以控制其它进程的访问权。
    这个函数很适合的。
      

  7.   

    简单方法:
    Private Function FileExists(filename) As Boolean  '判断文件是否存在
            FileExists = Not (Dir(filename) = Empty)
            Exit Function
    End Function'返回ture则文件存在
      

  8.   

    用dir函数或者FileSystemObject对象
    用filesystemobject找到某文件并删除之,然后再creatfile(),建一个相同文件名的空文件,