|
楼主 |
发表于 2011-9-1 19:57
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 huanglicheng 于 2011-9-1 21:04 编辑
求助了多个地方没有解决,自已组织了些代码效果还是实现了。可能还有些不足,先分享给大家参考参考
Sub Test() '使用双字典,旨在提高速度
Dim MyName$, I, MyFileName$, rr$
Dim d As Object, Dic As Object
Set d = CreateObject("scripting.dictionary")
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(0, "选择文件夹", &H10, 0)
If Not objFolder Is Nothing Then lj = objFolder.self.Path & "\" Else MsgBox "没有选择文件夹": Exit Sub
Set objFolder = Nothing
Set objShell = Nothing
On Error Resume Next
CommandBars("tmpContextMenu").Delete
Dim m As CommandBar
Set m = CommandBars.Add("tmpContextMenu", msoBarPopup)
Set regex1 = CreateObject("VBSCRIPT.REGEXP") 'RegEx为建立正则表达式
With regex1
.Global = True '设置全局可用
.Pattern = "[^\\]+"
End With
Set oSubMenu = m.Controls.Add(msoControlPopup)
oSubMenu.Caption = lj
Set oSubMenu2 = oSubMenu
Set Dic = CreateObject("Scripting.Dictionary") '创建一个字典对象
Set d = CreateObject("Scripting.Dictionary") '创建一个字典对象
Dic(lj) = 0
I = 0
Do While I < Dic.Count
Ke = Dic.keys '开始遍历字典
MyName = Dir(Ke(I), vbDirectory) '查找目录
Do While MyName <> ""
If MyName <> "." And MyName <> ".." Then
If (GetAttr(Ke(I) & MyName) And vbDirectory) = vbDirectory Then '如果是次级目录
Dic(Ke(I) & MyName & "\") = 0 '就往字典中添加这个次级目录名作为一个条目
End If
End If
MyName = Dir '继续遍历寻找
Loop
I = I + 1
Loop
For Each Ke In Dic.keys
MyFileName = Dir(Ke & "*.*")
Do While MyFileName <> ""
If MyFileName <> "." And MyFileName <> ".." Then
Set c1 = regex1.Execute(lj)
Set c = regex1.Execute(Ke & MyFileName)
If c.Count > c1.Count Then
For j = c1.Count To c.Count - 1
q = c.Item(j)
rr = rr & c.Item(j)
If Not d.exists(rr) Then
d(rr) = 0
If InStr(q, ".") = 0 Then
Set d(rr) = oSubMenu.Controls.Add(msoControlPopup)
Set oSubMenu = d(rr)
d(rr).Caption = q
Else
Set d(rr) = oSubMenu.Controls.Add(msoControlButton)
d(rr).Caption = q
End If
Else
Set oSubMenu = d(rr)
End If
Next j
Set oSubMenu = oSubMenu2
rr = ""
End If
End If
MyFileName = Dir
Loop
Next
m.ShowPopup
Set c1 = Nothing
Set c = Nothing
Set Dic = Nothing
Set d = Nothing
Set m = Nothing
Set oSubMenu = Nothing
Set oSubMenu2 = Nothing
Set regex1 = Nothing
End Sub生成效果图:
|
|