dir
or
findfirstfile, findnextfile

解决方案 »

  1.   

    try something like (adapted from 
    http://groups.google.com/groups?q=vb+dir+pattern&hl=zh-CN&lr=&ie=UTF-8&oe=UTF-8&selm=%23q4smoWlBHA.2168%40tkmsftngp04&rnum=8
    )Public Function FileList(ByVal Path As String, Optional Recursive As Boolean = False) As Collection
    Const Slash = "\", Dot = ".", Pattern = "*.rar"
    Dim Directories As Collection
    Dim tmp As String
    Dim PathLength As Long  'Init variables
      Set FileList = New Collection
      Set Directories = New Collection
      'Insure Path has trailing slash
      If Right$(Path, 1) <> Slash Then Path = Path & Slash
      PathLength = Len(Path)  'Check for files and directories in this Path
      'Trapped in case Path is bogus
      On Error Resume Next
      tmp = Path & Dir$(Path & Pattern, vbDirectory)
      On Error GoTo 0  'Loop for entire directory contents
      Do While Len(tmp) > PathLength
        If IsDirectory(tmp) Then
          'Save for later, refuse . and ..
          If Right$(tmp, 1) <> Dot Then Directories.Add tmp
        Else
          'Add file to listing
          FileList.Add tmp
        End If
        'Get next directory item
        tmp = Path & Dir$()
      Loop  'Catch sub directories
      If Recursive Then
      Dim tmpD, tmpF As Variant
        'Test each directory in this Path
        For Each tmpD In Directories
          'Recurse directory to add to files list
          For Each tmpF In FileList(tmpD, True)
            FileList.Add tmpF
          Next
        Next
      End IfEnd FunctionPublic Function IsDirectory(ByVal Path As String) As Boolean
      'Trapped in case Path is bogus
      On Error Resume Next
      IsDirectory = GetAttr(Path) And vbDirectory
    End Function
    ' To fill a listbox, you might do it this way:Private Sub Command1_Click()
    Dim File
      With List1
         .Clear
         .Visible = False
         For Each File In FileList("d:", True)
           .AddItem File
         Next
         .Visible = True
      End WithEnd Sub
      

  2.   

    dim fso as new filesystemobject 
    dim fil as file
    if fileexise(path)=true then
    msgbox "ok"
    else
    msgbox "cancel"
    end if
      

  3.   

    下面例子应当可以说明这个问题.------LOOKIN
    示例
    本示例可实现的功能为:在“My Documents”文件夹中查找文件名以“Cmd”开头的文件,并显示找到的每个文件的文件名及其所在位置。Set fs = Application.FileSearch
    With fs
        .LookIn = "C:\My Documents"
        .FileName = "cmd*.*"
        If .Execute > 0 Then
            MsgBox "There were " & .FoundFiles.Count & _
            " file(s) found."
            For i = 1 To .FoundFiles.Count
                MsgBox .FoundFiles(i)
            Next i
        Else
            MsgBox "There were no files found."
        End If
    End With