|
楼主 |
发表于 2011-7-9 12:05
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
关于遍历文件夹
http://club.excelhome.net/viewth ... p;extra=&page=1
老朽和大灰狼提供的方法。
注意45楼的更新,修正两处:
1、对打开文件夹目录的操作实现记忆功能;
2、对不同文件夹下的同名文件,容错
===================================================- Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
- Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
- Private Sub CommandButton1_Click()
- Set objShell = CreateObject("Shell.Application")
- Set objFolder = objShell.BrowseForFolder(0, "选择文件夹", 0, 0)
- If Not objFolder Is Nothing Then
- If Right(objFolder.self.Path, 1) = "" Then
- TextBox1 = objFolder.self.Path
- Else
- TextBox1 = objFolder.self.Path & ""
- End If
- End If
- Set objFolder = Nothing
- Set objShell = Nothing
- End Sub
- Private Sub CommandButton2_Click()
- Unload Me
- End Sub
- Private Sub CommandButton3_Click()
- Call 建立资料目录
- End Sub
- Private Sub UserForm_Initialize()
- Dim i As Integer
- Dim temp As String * 255
- i = GetPrivateProfileString("LastFindInfo", "Folder", "c:", temp, 255, "ExcelFind.ini")
- TextBox1 = temp
- End Sub
- Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) '保存上次打开文件夹路径
- Dim i As Integer
- Dim temp As String * 255
- temp = TextBox1
- i = WritePrivateProfileString("LastFindInfo", "Folder", temp, "ExcelFind.ini")
- End Sub
- Sub 建立资料目录() '使用双字典,旨在提高速度
- Dim yy()
- Dim xx '(1 To 1000, 1 To 4)
- Dim MyName, Dic, Did, i, T, F, TT, MyFileName, objShell, objFolder, lj, Ke, sz, Sh, rng As Range, cell As Range
- Dim ObjFso As New FileSystemObject
- 'On Error Resume Next
- 'Set objShell = CreateObject("Shell.Application")
- 'Set objFolder = objShell.BrowseForFolder(0, "选择文件夹", 0, 0)
- 'If Not objFolder Is Nothing Then lj = objFolder.self.Path & ""
- 'Set objFolder = Nothing
- 'Set objShell = Nothing
- lj = TextBox1
- Application.ScreenUpdating = False
- T = Timer
- Set Dic = CreateObject("Scripting.Dictionary") '创建一个字典对象
- Set Did = CreateObject("Scripting.Dictionary")
- Dic.Add (lj), ""
- 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.Add (Ke(i) & MyName & ""), "" '就往字典中添加这个次级目录名作为一个条目
- End If
- End If
- MyName = Dir '继续遍历寻找
- Loop
- i = i + 1
- Loop
- sz = Split(lj, "")
- If UBound(sz) = 1 Then
- ls = Left(lj, 1) & "盘"
- Else
- ls = sz(UBound(sz) - 1)
- End If
- nTotalLine = 0
- p = 1
- i_p = 1
- Did.Add (ls & "文件清单"), "" '以查找D盘下所有EXCEL文件为例
- For Each Ke In Dic.keys
- 'xx(p, 1) = Ke
- 'xx(p, 4) = Ke
- ReDim Preserve yy(p)
- yy(p) = Ke & "|" & " " & "|" & " " & "|" & Ke
- p = p + 1
- Did.Add Ke, ""
- MyFileName = Dir(Ke & "*.*")
- Do While MyFileName <> ""
- If Did.Exists(MyFileName) Then
- Did.Add (Ke & MyFileName), ""
- Else
- Did.Add (MyFileName), ""
- End If
- ' xx(p, 1) = MyFileName
- ' xx(p, 2) = Round(FileLen(Ke & MyFileName) / 1024, 2) & "K"
- Set ObjFile = ObjFso.GetFile(Ke & MyFileName)
- ' xx(p, 3) = ObjFile.DateLastModified '最后修改日期
- ' xx(p, 4) = Ke & MyFileName
- ReDim Preserve yy(p)
- yy(p) = MyFileName & "|" & Round(FileLen(Ke & MyFileName) / 1024, 2) & "K" & "|" & ObjFile.DateLastModified & "|" & Ke & MyFileName
- p = p + 1
- MyFileName = Dir
- Loop
- ' Did.Add "", ""
- ' p = p + 1
- Next
- For Each Sh In ThisWorkbook.Worksheets
- If Sh.Name = ls & "文件清单" Then
- Sheets(ls & "文件清单").Cells.Delete
- F = True
- Exit For
- Else
- F = False
- End If
- Next
- If Not F Then
- Sheets.Add.Name = ls & "文件清单"
- End If
- 'Sheets(ls & "文件清单").[A1].Resize(Did.Count, 1) = WorksheetFunction.Transpose(Did.keys)
- With Sheets(ls & "文件清单")
- .Activate
- .Cells(1, 1) = "路径文件名"
- .Cells(1, 2) = "文件大小"
- .Cells(1, 3) = "时间属性"
- ReDim xx(1 To p - 1, 1 To 4)
- For i = 1 To p - 1
- a = Split(yy(i), "|")
- For j = 0 To 3
- xx(i_p, j + 1) = a(j)
- Next
- i_p = i_p + 1
- Next
- .Range("a2:c" & i_p) = xx
- j = Did.Count
- Set rng = .Range("a1:c" & j)
- For s = 1 To j - 1
- .Cells(s + 1, 1).Select
- .Cells(s + 1, 1).Hyperlinks.Add Anchor:=Selection, Address:=xx(s, 4) '.Cells(S, 1)
- rng.EntireColumn.AutoFit
- Next s
- End With
- Set rng = Nothing
- ThisWorkbook.SaveAs ThisWorkbook.Path & "" & ls & "文件清单"
- Application.ScreenUpdating = True
- TT = Timer - T
- MsgBox TT 'Minute(TT) & "分" & Second(TT) & "秒"
- End Sub
复制代码
[ 本帖最后由 lrlxxqxa 于 2011-7-9 12:07 编辑 ] |
|