sushma rajaraman
sushma rajaraman

Reputation: 31

To search for a last updated folder in a directory using VBA

How to get the last updated folder in a directory? Say for example I have a folder C:\test and it has many folders inside. I need the latest folder's name

Upvotes: 0

Views: 2906

Answers (2)

Pupa Rebbe
Pupa Rebbe

Reputation: 528

this should work.

Function GetLastFolder(Path As String)
    Dim FSO, FS, F, DtLast As Date, Result As String
    Set FSO = CreateObject("scripting.FileSystemObject")
    Set FS = FSO.GetFolder(Path).SubFolders
    For Each F In FS
        If F.DateLastModified > DtLast Then
             DtLast = F.DateLastModified
             Result = F.Name
        End If
    Next
    GetLastFolder = Result
End Function

you can call the function like this:

GetLastFolder("c:\test")

Upvotes: 1

daZza
daZza

Reputation: 1689

One possible approach:

Use the FileDateTime(path) function and then build a loop around it. Store the name and time from the first subfolder in respective variables and then compare the time against that variable with each pass of the loop. If it's newer, store the new name / time value, else next loop pass.

After the loop is completed your variables will hold the subfolder with the latest modification date.

Upvotes: 0

Related Questions