如何知道VBA中最后修改的文件夹的名称?

问题描述 投票:0回答:1
Set fso = CreateObject("Scripting.FileSystemObject")
Set froot = fso.GetFolder(strstartfldr)

For Each fldr In froot.SubFolders
    UserForm1.ComboBox1.AddItem fldr.DateCreated
Next

我可以获取组合框1中的子文件夹列表,但是从这个子文件夹中我如何知道哪个是最后修改的文件夹。

vba directory subdirectory last-modified fso
1个回答
0
投票

最后一个文件夹 (FSO)

  • 以下函数获取上次创建的文件夹的名称。

代码

Option Explicit

Function LastFolder(FolderPath As String) As String
    
    Dim fso As Object
    Dim froot As Object
    Dim fldr As Object
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set froot = fso.GetFolder(FolderPath)
    Dim fName As String
    Dim fDC As Date
    Dim TMP As Date
    For Each fldr In froot.subfolders
        TMP = fldr.DateCreated
        If TMP > fDC Then
            fDC = TMP
            fName = fldr.Name
        End If
    Next fldr
    
    LastFolder = fName

End Function

Sub testLastFolder()
    Debug.Print LastFolder("F:\StackOverFlow")
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.