vb 搜索指定目录下的指定类型文件

ExpandedBlockStart.gif 代码
Function  SearchFiles(Path  As   String , FileType  As   String )
    
On   Error   GoTo  ErrMsg
    
Dim  Files()  As   String                 ' 文件路径
     Dim  Folder()  As   String                ' 文件夹路径
     Dim  a, b, c  As   Long
    
Dim  sPath  As   String
    
    
If   Right (Path,  1 <>   " \ "   Then  Path  =  Path  &   " \ "
    
    sPath 
=  Dir(Path  &  FileType)         ' 查找第一个文件
    
    
Do   While   Len (sPath)                  ' 循环到没有文件为止
       a  =  a  +   1
       
ReDim  Preserve Files( 1   To  a)
       Files(a) 
=  Path  &  sPath           ' 将文件目录和文件名组合,并存放到数组中
       listFileName.AddItem Files(a)     ' 加入list控件中
       sPath  =  Dir                       ' 查找下一个文件
       DoEvents                          ' 让出控制权
     Loop
    
    sPath 
=  Dir(Path  &   " \ " , vbDirectory)  ' 查找第一个文件夹
    
    
Do   While   Len (sPath)                  ' 循环到没有文件夹为止
        If   Left (sPath,  1 <>   " . "   Then      ' 为了防止重复查找
         If  GetAttr(Path  &   " \ "   &  sPath)  And  vbDirectory  Then   ' 如果是文件夹则。。。。。。
           b  =  b  +   1
           
ReDim  Preserve Folder( 1   To  b)
           Folder(b) 
=  Path  &  sPath  &   " \ "   ' 将目录和文件夹名称组合形成新的目录,并存放到数组中
         End   If
       
End   If
     sPath 
=  Dir                         ' 查找下一个文件夹
     DoEvents                            ' 让出控制权
     Loop
    
    
For  c  =   1   To  b                       ' 使用递归方法,遍历所有目录
       SearchFiles Folder(c), FileType
    
Next
ErrMsg:
    
End Function

 

转载于:https://www.cnblogs.com/lano/archive/2009/12/09/1620097.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值