Salek
Salek

Reputation: 303

Find the last folder named as month from subfolders

I have reports grouped by months in folder tree

"somepath\YYYY\MMMYYYY"

For example "somepath\2017\MAR2017". Obviously, there may be up to 12 folders "MMMYYYY" in the Year folder. System uploads files from the last folder identified by month in its name. I.e. among folders Jan2017, Feb2017, Mar2017 it should upload files from Mar2017.

I tried convert the foldername to number:

Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
Dim ObjFolder : Set ObjFolder = fso.GetFolder("somepath\2017\")
Set fc = ObjFolder.SubFolders 'here I should get all folders with months in their names
For each f in fc
a = Month("01-"&left(f.name, 3)&"-"&right(f.name, 4))
print a
Next

it prints next:

4

2

3

Next step should be identifying the greatest number from these and set working folder name as

a = MonthName(maxnumber)&"2017"
workingfolder = "somepath\2017\"&a&"\"

How to identify that max number or max month?

Upvotes: 1

Views: 57

Answers (1)

Salek
Salek

Reputation: 303

The following worked:

Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
Dim ObjFolder : Set ObjFolder = fso.GetFolder("somepath\2017\")
Set fc = ObjFolder.SubFolders
    For i=0 to fc.count
        For each f in fc
            a = Month("01-"&left(f.name, 3)&"-"&right(f.name, 4))
            Redim Preserve arr(fc.count)
            arr(i)=a
        Next
    Next
    arrLen = UBound(arr) 'Find the length of array
    For j= 0 to arrLen
     If arr(j) > max Then
        max=arr(j)
     End If
    Next
Dim OrgFolder : Set OrgFolder = fso.GetFolder(ObjFolder&"\"&MonthName(arr(0), True)&"2017\")

Upvotes: 1

Related Questions