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
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