Create a new project with a form containing four text boxes (Text1, Text2, Text3, Text4), a check boxes (Check1), a list box (List1) and a command button (Command1). Label as desired and add the following code: --------------------------------------------------------------------------------
 
Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2002 VBnet, Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you many not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source on 
'               any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Const vbDot = 46
Private Const MAXDWORD = &HFFFFFFFF
Private Const MAX_PATH = 260
Private Const INVALID_HANDLE_VALUE = -1
Private Const FILE_ATTRIBUTE_DIRECTORY = &H10Private 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 TypePrivate Type FILE_PARAMS
   bRecurse As Boolean
   sFileRoot As String
   sFileNameExt As String
   sResult As String
   sMatches As String
   Count As Long
End TypePrivate Declare Function FindClose Lib "kernel32" _
  (ByVal hFindFile As Long) As Long
   
Private Declare Function FindFirstFile Lib "kernel32" _
   Alias "FindFirstFileA" _
  (ByVal lpFileName As String, _
   lpFindFileData As WIN32_FIND_DATA) As Long
   
Private Declare Function FindNextFile Lib "kernel32" _
   Alias "FindNextFileA" _
  (ByVal hFindFile As Long, _
   lpFindFileData As WIN32_FIND_DATA) As LongPrivate Declare Function GetTickCount Lib "kernel32" () As LongPrivate Sub Command1_Click()   Dim FP As FILE_PARAMS  'holds search parameters
   Dim tstart As Single   'timer var for this routine only
   Dim tend As Single     'timer var for this routine only
   
  'setting the list visibility to false
  'increases the load time
   Text3.Text = ""
   List1.Clear
   List1.Visible = False
   
  'set up search params
   With FP
      .sFileRoot = Text1.Text       'start path
      .sFileNameExt = Text2.Text    'file type of interest
      .bRecurse = Check1.Value = 1  '1 = recursive search
   End With
   
  'get start time, get files, and get finish time
   tstart = GetTickCount()
   Call SearchForFiles(FP)
   tend = GetTickCount()
   
   List1.Visible = True
   
  'show the results
   Text3.Text = Format$(FP.Count, "###,###,###,##0") & _
                        " found (" & _
                        FP.sFileNameExt & ")"
                   
   Text4.Text = FormatNumber((tend - tstart) / 1000, 2) & "  seconds"
                                    
End Sub
Private Sub GetFileInformation(FP As FILE_PARAMS)  'local working variables
   Dim WFD As WIN32_FIND_DATA
   Dim hFile As Long
   Dim sPath As String
   Dim sRoot As String
   Dim sTmp As String
      
  'FP.sFileRoot contains the path to search.
  'FP.sFileNameExt contains the full path and filespec.
   sRoot = QualifyPath(FP.sFileRoot)
   sPath = sRoot & FP.sFileNameExt
   
  'obtain handle to the first filespec match
   hFile = FindFirstFile(sPath, WFD)
   
  'if valid ...
   If hFile <> INVALID_HANDLE_VALUE Then      Do
         
        'Even though this routine uses file specs,
        '*.* is still valid and will cause the search
        'to return folders as well as files, so a
        'check against folders is still required.
         If Not (WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = _
                 FILE_ATTRIBUTE_DIRECTORY Then           'this is where you add code to store
           'or display the returned file listing.
           '
           'if you want the file name only, save 'sTmp'.
           'if you want the full path, save 'sRoot & sTmp'           'remove trailing nulls
            FP.Count = FP.Count + 1
            sTmp = TrimNull(WFD.cFileName)
            List1.AddItem sRoot & sTmp         End If
         
      Loop While FindNextFile(hFile, WFD)
      
      
     'close the handle
      hFile = FindClose(hFile)
   
   End IfEnd Sub
Private Sub SearchForFiles(FP As FILE_PARAMS)  'local working variables
   Dim WFD As WIN32_FIND_DATA
   Dim hFile As Long
   Dim sPath As String
   Dim sRoot As String
   Dim sTmp As String
      
   sRoot = QualifyPath(FP.sFileRoot)
   sPath = sRoot & "*.*"
   
  'obtain handle to the first match
   hFile = FindFirstFile(sPath, WFD)
   
  'if valid ...
   If hFile <> INVALID_HANDLE_VALUE Then
   
     'This is where the method obtains the file
     'list and data for the folder passed.
      Call GetFileInformation(FP)      Do
      
        'if the returned item is a folder...
         If (WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) Then
            
           '..and the Recurse flag was specified
            If FP.bRecurse Then
            
              'and if the folder is not the default
              'self and parent folders (a . or ..)
               If Asc(WFD.cFileName) <> vbDot Then
               
                 '..then the item is a real folder, which
                 'may contain other sub folders, so assign
                 'the new folder name to FP.sFileRoot and
                 'recursively call this function again with
                 'the amended information.                 'remove trailing nulls
                  FP.sFileRoot = sRoot & TrimNull(WFD.cFileName)
                  Call SearchForFiles(FP)
                  
               End If
               
            End If
            
         End If
         
     'continue looping until FindNextFile returns
     '0 (no more matches)
      Loop While FindNextFile(hFile, WFD)
      
     'close the find handle
      hFile = FindClose(hFile)
   
   End If
   
End Sub
Private Function QualifyPath(sPath As String) As String  'assures that a passed path ends in a slash
   If Right$(sPath, 1) <> "\" Then
         QualifyPath = sPath & "\"
   Else: QualifyPath = sPath
   End If
      
End Function
Private Function TrimNull(startstr As String) As String  'returns the string up to the first
  'null, if present, or the passed string
   Dim pos As Integer
   
   pos = InStr(startstr, Chr$(0))
   
   If pos Then
      TrimNull = Left$(startstr, pos - 1)
      Exit Function
   End If
  
   TrimNull = startstr
  
End Function
'--end block--'
 

解决方案 »

  1.   

    别人不热心,我来告诉你:if dir$(文件全名)="" then
       msgbox "制定文件不存在"
    end if
      

  2.   

    大家都不知道这个函数吗?Declare Function SearchPath Lib "kernel32" Alias "SearchPathA" (ByVal lpPath As String, ByVal lpFileName As String, ByVal lpExtension As String, ByVal nBufferLength As Long, ByVal lpBuffer As String, ByVal lpFilePart As String) As Long查找指定文件
    Long,装载到lpBuffer缓冲区的字符数。如缓冲区长度不足,则返回缓冲区必要的长度。零表示失败。会设置GetLastErrorlpPath ---------  String,欲搜索的路径。如果为vbNullString,则采用windows搜索路径。参考OpenFile函数的OFSTRUCT结构中对OF_SEARCH标志搜索顺序的介绍  lpFileName -----  String,要查找的文件名  lpExtension ----  String,文件扩展名。必须用一个句点符号起头。如文件没有扩展名,或者lpFileName包括了扩展名,则设为vbNullString  nBufferLength --  Long,lpBuffer字串的长度  lpBuffer -------  String,用于装载文件名的一个字串  lpFilePart -----  String,指定一个长整数变量,用于装载缓冲文件名部分的地址。在vb中不是特别有用参考GetFullPathName函数
      

  3.   

    感谢您使用微软产品。 您可以使用Windows API或者VB的文件操作函数两种方式,搜索指定目录及其子目录下的文件或文件夹。详细信息和示例代码清参考下面的文章:
    HOWTO: Search Directories to Find or List Files (Q185476)
    http://support.microsoft.com/default.aspx?scid=kb;en-us;Q185476
    - 微软全球技术中心 VB技术支持
    本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款
    (http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
    为了为您创建更好的讨论环境,请参加我们的用户满意度调查
    (http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。