Hi, Ed,
I can show you how to get close to what you want. In fact, I'll show you two
different ways to do it. The last little bit is a problem, though. There's
no guarantee that the code will retrieve the folder names in the order they
were created, or in any sorted order. And if you do sort them
alphanumerically, you'll still get them out of date order -- for example,
"Folder 01 Dec 2003" will sort *before* "Folder 23 Nov 2003". You'd have to
put in a lot of effort to sort by date order and figure out which one is the
last.
Anyway, play with the following two macros. The first one is an extension of
the one I showed before. The "trick" is that after calling Dir$() with a
pattern, you can call it again with empty parentheses, and it will return
the next match to the pattern, or "" if there is no match.
Sub foo1()
Dim FolderName As String
Dim FolderList As String
Dim FolderCount As Integer
FolderCount = 0
FolderName = Dir$("C:\Folder *", vbDirectory)
Do While (FolderName <> "")
FolderList = FolderList & FolderName & vbCr
FolderCount = FolderCount + 1
FolderName = Dir$()
Loop
If FolderCount = 0 Then
MsgBox "Not found"
ElseIf FolderCount = 1 Then
FolderName = FolderList
MsgBox "only one: " & FolderName
Else
MsgBox "Delete unused folders from this list:" _
& vbCr & FolderList
End If
End Sub
For the second one, you should first go to the Tools > References dialog in
the VBA editor and put a check next to "Microsoft Scripting Runtime". That
makes available the FileScriptingObject, which has more capabilities than
Dir$() and is usually faster.
Sub foo2()
Dim FolderName As String
Dim oFSO As FileSystemObject
Dim oFldr As Folder, oSubFldr As Folder
Dim SFlist As String, SFcount As Integer
Set oFSO = New FileSystemObject
Set oFldr = oFSO.GetFolder("C:\")
If Not oFldr Is Nothing Then
SFcount = 0
For Each oSubFldr In oFldr.SubFolders
If oSubFldr.Name Like "Folder *" Then
SFlist = SFlist & oSubFldr.Name & vbCr
SFcount = SFcount + 1
End If
Next oSubFldr
If Right(SFlist, 1) = vbCr Then
SFlist = Left(SFlist, Len(SFlist) - 1)
End If
If SFcount = 0 Then
MsgBox "Not found"
ElseIf SFcount = 1 Then
FolderName = SFlist
MsgBox "only one: " & FolderName
Else
MsgBox "Delete unused folders from this list:" _
& vbCr & SFlist
End If
End If
End Sub