51Testing软件测试论坛

标题: 查找一个文件夹下(包括子文件夹)的所有后缀是.TXT的文件. [打印本页]

作者: flowerruirui    时间: 2011-7-28 21:17
标题: 查找一个文件夹下(包括子文件夹)的所有后缀是.TXT的文件.
问题是查找一个文件夹下(包括子文件夹)的所有后缀是.TXT的文件,统计有多少个并把这些文件的绝对路径写入一个文件.


Dim fso,fol
fol="D:\QTP"   '假设查找的是D:\QTP文件夹下(包括子文件夹)的所有后缀是.TXT的文件

On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")

'对文件夹进行子文件夹的遍历
Function GF(fol)
  Wh( fol )    '得到D:\QTP文件夹后缀是.TXT的文件
  Dim i
  For Each i In fol.SubFolders   
  GF i
  Wh( fol )     '得到D:\QTP子文件夹后缀是.TXT的文件
  Next
End Function


'文件夹下的.TXT的文件
Function Wh(fol)
  Dim i , counter
  counter=0
  For Each i In fol.Files
    If LCase(fso.GetExtensionName(i)) = ".txt" Then
    strGetPath= fso.GetAbsolutePathName
        fso.OpenTexfile("D:\result.txt",8, true).write strGetPath  '得到.TXT的文件的绝对路径,并写入D:\result.txt

          counter= counter+1
          msgbox counter      '统计有多少个.TXT的文件

    End If
  Next
End Function

Call GF(fol)



上面的代码运行后确无返回结果,请问一下怎么才能得到我想要的结果
作者: shanfeng1419    时间: 2011-7-29 16:59
本帖最后由 shanfeng1419 于 2011-7-29 17:00 编辑

Dim total_count, sPath
total_count=0
sPath= "F:\Program Files"
total_count = FilesTree(sPath)
print total_count

Function FilesTree (sPath)

    Set oFso = CreateObject("Scripting.FileSystemObject")
    Set oFolder = oFso.GetFolder(sPath)
    Set oSubFolders = oFolder.SubFolders   
    Set oFiles = oFolder.Files

            For each f in oFolder.Files
               na = f.Name
               na = right(na,4)
               If na =".txt" Then
                        total_count  = total_count + 1
               End If   
             next
                ' print "there is(are) " ∑ &" .txt files in the " & oFolder.path& " folders"

              For Each oSubFolder In oSubFolders  '子文件夹中txt个数  递归
                FilesTree(oSubFolder.Path)'递归
             Next
             FilesTree = total_count
End Function
大家还有没有好办法,学习学习。
作者: 17800455    时间: 2011-7-30 22:34
很明显的少了一层。
作者: 17800455    时间: 2011-7-30 22:34
很明显的少了一层。
作者: shanfeng1419    时间: 2011-8-1 08:12
3楼的你好,是说我的的少了一层么,能分享下你的思路么
作者: 阳光下的橙子    时间: 2011-8-1 14:44
本帖最后由 阳光下的橙子 于 2011-8-1 14:50 编辑

我也写了一段脚本,能实现对文件夹下各种后缀文件的遍历,包括根目录和子目录

folderspec = 文件夹路径

Function ShowFilename(folderspec)
   Dim fso,f,f1,sf,k,str,fs,sfd,filetype
   filetype = LCase(InputBox ("请输入你想要搜索的文件后缀名,比如txt","搜索自定义"))
   k = 0
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.GetFolder(folderspec)
   Set fs = f.SubFolders
   
   If fs.Count > 0 Then
   Num = fs.Count
   MsgBox "存在"&Num&"个子文件夹", ,"文件夹搜索提示"
   For Each sfd In fs
      For Each f1 In sfd.Files
      str = ""
      i = Len(f1)
      For j = i-2  To i
      str = str & Mid(f1,j,1)
      Next
      If str = filetype Then
      k = k+1
      End If
      Next
   Next
      Set sf = f.Files
   For Each f1 in sf
      str = ""
      i = Len(f1)
      For j = i-2  To i
      str = str & Mid(f1,j,1)
      Next
      If str = filetype Then
      k = k+1
      End if
   Next
   
   Else
   MsgBox "不存在子文件夹,直接遍历根目录", ,"文件夹搜索提示"
      Set sf = f.Files
   For Each f1 in sf
      str = ""
      i = Len(f1)
      For j = i-2  To i
      str = str & Mid(f1,j,1)
      Next
      If str = filetype Then
      k = k+1
      End if
   Next
   End If
   
   MsgBox "一共有"&k&"个"&filetype&"文件!"
End Function




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2