判断一个文件是否存在:
Private Sub Command1_Click()
    MsgBox IIf(Dir("C:\autoexec.bat", vbNormal) <> "", "文件存在", "文件不存在")
End Sub

解决方案 »

  1.   

    http://www.csdn.net/expert/Topic/192/192649.shtmhttp://www.csdn.net/expert/Topic/225/225327.shtm
      

  2.   

    如果要判断硬盘中一个文件是否存在,需要编写递归函数查找,我的函数为:
    Private Sub RecurseTree(CurrentPath As String)
        Dim nI As Integer, nDirectory As Integer
        Dim sFileName As String, sDirectoryList() As String
        On Error Resume Next
        sFileName = Dir(CurrentPath)
        Do While sFileName <> ""
            '''注意:这里是判断条件
            If UCase(sFileName) = "FOXMAIL.EXE" Then
               EmailPath = CurrentPath & sFileName
               Exit Sub
            End If
            sFileName = Dir
        Loop
        sFileName = Dir(CurrentPath, vbDirectory)
        Do While sFileName <> ""
            If sFileName <> "." And sFileName <> ".." Then
                If GetAttr(CurrentPath & sFileName) And vbDirectory Then
                    nDirectory = nDirectory + 1
                    ReDim Preserve sDirectoryList(nDirectory)
                    sDirectoryList(nDirectory) = CurrentPath & sFileName
                End If
            End If
            sFileName = Dir
            DoEvents
        Loop
        For nI = 1 To nDirectory
            RecurseTree sDirectoryList(nI) & "\"
        Next nI
    End Sub
    这样可以判断currentpath目录下是否存在foxmail.exe文件,如果有就可以存到EmailPath变量中,而且可以访问currentpath的子目录,所以只要设置currentpath为硬盘根目录,就可以查找这个硬盘下有没有某个文件了。