我正在尝试编写代码,以便跨子文件夹收集类似名称的文件。例如,主文件夹是" Reports"具有子文件夹" Reports \ 2015" (和其他年份),每一个都是一个月" Reports \ 2015 \ 01 jan 15"在每个文件中都有一个名为" Reports \ 2015 \ 01 jan 15 \ DSR"的文件。给定开始月份和年份以及结束月份和年份我尝试收集名称为DSR的文件。我有所有设置的用户,但我坚持这个。任何想法?谢谢这是我到目前为止所拥有的
编辑:我更新了代码
Public Sub NonRecursiveMethod()
Dim fso, oFolder, oSubfolder, oFile, queue As Collection
Dim years
Dim yeare
Dim months
Dim monthe
Dim Name
Dim path1() As String
years = "2012"
yeare = "2015"
months = "05"
monthe = "09"
Name = "DSR"
Dim counter
Set fso = CreateObject("Scripting.FileSystemObject")
Set queue = New Collection
queue.Add fso.getFolder("L:\Live\OES\DAILY OLD\") 'obviously replace
i = 1
j = 1
q = 0
Do While queue.Count > 0
Set oFolder = queue(1)
queue.Remove 1 'dequeue
'...insert any folder processing code here...
yearspot = Val(Mid(oFolder, 23, 4))
monthspot = Val(Mid(oFolder, 28, 2))
If yearspot = Val(years) Then
If monthspot >= Val(months) Then
Cells(i + 1, 1) = Mid(oFolder, 23, 4)
Cells(i + 1, 2) = Mid(oFolder, 28, 2)
ReDim Preserve path1(i)
path1(i) = oFolder
Cells(i + 1, 3) = path1(i)
i = i + 1
End If
ElseIf yearspot = Val(yeare) Then
If monthspot <= Val(monthe) Then
If monthspot > 0 Then
' MsgBox yearspot
' MsgBox monthspot
Cells(i + 1, 1) = Mid(oFolder, 23, 4)
Cells(i + 1, 2) = Mid(oFolder, 28, 2)
ReDim Preserve path1(i)
path1(i) = oFolder
Cells(i + 1, 3) = path1(i)
i = i + 1
End If
End If
Else
If yearspot < Val(yeare) Then
If yearspot > Val(years) Then
If monthspot >= 1 Then
If monthspot <= 12 Then
Cells(i + 1, 1) = Mid(oFolder, 23, 4)
Cells(i + 1, 2) = Mid(oFolder, 28, 2)
ReDim Preserve path1(i)
path1(i) = oFolder
Cells(i + 1, 3) = path1(i)
i = i + 1
End If
End If
End If
End If
End If
For Each oSubfolder In oFolder.SubFolders
queue.Add oSubfolder 'enqueue
Next oSubfolder
For Each oFile In oFolder.Files
'...insert any file processing code here...
' For counter = 1 To UBound(path1)
' If path1(counter) = Mid(oFile, 1, 30) Then
' MsgBox path1(counter)
' End If
' Next counter
Next oFile
Loop
End Sub