2017-07-17 69 views
1

我有個文件夾樹查找命名爲一個月子文件夾的最後一個文件夾

「somepath \ YYYY \ MMMYYYY」

例如"somepath\2017\MAR2017"分組報告。 顯然,Year文件夾中最多可能有12個文件夾"MMMYYYY"。 系統從名稱中的月份標識的上一個文件夾中上載文件。即在文件夾Jan2017,Feb2017,Mar2017它應該從Mar2017上傳文件。

我嘗試了文件夾名轉換成數:

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 

它打印下一個:

下一步應該從這些標識最大數字並設置工作文件夾名稱爲

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

如何識別最大數量或最大月數?

回答

1

以下工作:

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\") 
相關問題