【发布时间】:2015-12-17 17:59:44
【问题描述】:
我有一个循环遍历所有目录的代码,但我只需要在每个级别循环遍历一些特定目录。例如路径。 C:/主目录/ABC*/Y/XYZ*/*.edf.
此代码通过递归给出每个目录中的每个文件。我无法对其进行编辑,使其仅提供具有单个模式的子目录和具有单个模式的该文件夹的子目录,然后是具有另一个单一模式的子目录,然后仅提供其中的 .edf 文件文件夹。我可以在这段代码中做 .edf 文件的事情
我是通过这两个函数来做的。
Function Recursive(FolderPath As String)
Dim Value As String, Folders() As String
Dim Folder As Variant, a As Long
ReDim Folders(0)
If Right(FolderPath, 2) = "\\" Then Exit Function
Value = Dir(FolderPath, &H10)
Do Until Value = ""
If Value = "." Or Value = ".." Then
Else
If GetAttr(FolderPath & Value) = 16 Then
Folders(UBound(Folders)) = Value
ReDim Preserve Folders(UBound(Folders) + 1)
Else
If Count = 4 Then
temp(0, UBound(temp, 2)) = FolderPath
temp(1, UBound(temp, 2)) = Value
temp(2, UBound(temp, 2)) = Count ' FileLen(FolderPath & Value)
ReDim Preserve temp(UBound(temp, 1), UBound(temp, 2) + 1)
End If
End If
End If
Value = Dir
Loop
For Each Folder In Folders
Count = Count + 1
Recursive FolderPath & Folder & "\"
Count = Count - 1
Next Folder
End Function
和
Public temp() As String
Public Count As Integer
Function ListFiles(FolderPath As String)
Dim k As Long, i As Long
ReDim temp(2, 0)
Count = 1
If Right(FolderPath, 1) <> "\" Then
FolderPath = FolderPath & "\"
End If
Recursive FolderPath
k = Range(Application.Caller.Address).Rows.Count
If k < UBound(temp, 2) Then
MsgBox "There are more rows, extend user defined function"
Else
For i = UBound(temp, 2) To k
ReDim Preserve temp(UBound(temp, 1), i)
temp(0, i) = ""
temp(1, i) = ""
temp(2, i) = ""
Next i
End If
ListFiles = Application.Transpose(temp)
ReDim temp(0)
End Function
【问题讨论】:
-
您好,欢迎来到 Stack Oberflow。在发布之前,请花时间阅读我们帮助中的mcve 部分。
-
感谢您的评论。显然这是我的第一个问题。您能否指出问题的问题所在。我已经浏览了帮助部分,但我无法弄清楚。
-
没有实际问题被问到。您在使用的脚本中报告了哪些错误?
-
@nbayly 此代码用于循环遍历所有目录和文件。我无法对其进行编辑,使其仅提供具有单个模式的子目录和具有单个模式的该文件夹的子目录,然后是具有另一个单一模式的子目录,然后仅提供其中的 .edf 文件文件夹。我可以在这段代码中做 .edf 文件的事情。