3
這是我第一次問問題,所以希望我遵循協議。 這是參照「獲取vba中的子目錄列表」get list of subdirs in vba。Excel VBA使用FileSystemObject列出文件上次修改日期
我發現Brett的例子#1 - 使用FileScriptingObject最有幫助。但是在結果中還需要另外一個數據元素(DateLastModified)。我試圖修改代碼,但不斷收到無效的限定符錯誤。這裏是我所做的代碼修改:
- 範圍(「A1:C1」)=數組(「文件名」,「路徑」,「上次修改日期」)。
- Do While loop added this => Cells(i,3)= myFile.DateLastModified。
對包含「上次修改日期」的幫助很感激。
這裏的Santosh是完整的代碼,其註釋表示修改。
Public Arr() As String
Public Counter As Long
Sub LoopThroughFilePaths()
Dim myArr
Dim i As Long
Dim j As Long
Dim MyFile As String
Const strPath As String = "c:\temp\"
myArr = GetSubFolders(strPath)
Application.ScreenUpdating = False
'Range("A1:B1") = Array("text file", "path")' <= orig code
Range("A1:C1") = Array("text file", "path", "Date Last Modified") ' <= modified code
For j = LBound(Arr) To UBound(Arr)
MyFile = Dir(myArr(j) & "\*.txt")
Do While Len(MyFile) <> 0
i = i + 1
Cells(i, 1) = MyFile
Cells(i, 2) = myArr(j)
Cells(i, 3) = MyFile.DateLastModified ' <= added to modify code
MyFile = Dir
Loop
Next j
Application.ScreenUpdating = True
End Sub
Function GetSubFolders(RootPath As String)
Dim fso As Object
Dim fld As Object
Dim sf As Object
Dim myArr
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(RootPath)
For Each sf In fld.SubFolders
Counter = Counter + 1
ReDim Preserve Arr(Counter)
Arr(Counter) = sf.Path
myArr = GetSubFolders(sf.Path)
Next
GetSubFolders = Arr
Set sf = Nothing
Set fld = Nothing
Set fso = Nothing
End Function
ü可以把完整的代碼與你所做的更改一起? – Santosh
Santosh下面是完整的代碼以及修改 – user2397403