How to find specific subfolders

G

Guest

I need to make a macro that searches for subfolders which names start with
four digit numbers and count them. To clarify, I have a folder structure like:

MainFolder
CountryFolder
1989
more subfolders
2001-02
more subfolders
Other country folder
1976
....

Problem is I know how to search *files* with specific criteria using
filesearch, but not how to search for *subfolders* that match criteria, in
this case, the 4 first characters of the subfolder name are numeric. I always
get confused with searchscopes and scopefolders. Can someone shed some light
on this?

Thanks in advance,

Rafael
 
B

Bob Phillips

Rafael,

Try this

Private cnt1 As Long
Private cnt2 As Long

Sub Folders()
Dim i As Long
Dim sFolder As String

cnt1 = 0
cnt2 = 0
sFolder = "C:\MyTest"
If sFolder <> "" Then
SelectFiles sFolder
End If
MsgBox cnt2 & " out of " & cnt1 & " match"

End Sub

'-----------------------------------------------------------------------
Sub SelectFiles(Optional sPath As String)
'-----------------------------------------------------------------------
Static FSO As Object
Dim oSubFolder As Object
Dim oFolder As Object

If FSO Is Nothing Then
Set FSO = CreateObject("SCripting.FileSystemObject")
End If

cnt1 = cnt1 + 1
If IsNumeric(Left(sPath, 4)) Then
cnt2 = cnt2 + 1
End If

Set oFolder = FSO.Getfolder(sPath)
For Each oSubFolder In oFolder.Subfolders
SelectFiles oSubFolder.Path
Next

End Sub



--

HTH

RP
(remove nothere from the email address if mailing direct)
 
G

Guest

Hi Bob!

Thanks for the suggestion and sample code, it almost worked perfectly on the
first run, I made some minor adjustments and got the job done.

The problem was with:

If IsNumeric(Left(sPath, 4)) Then

Because the path of a subfolder such as H:\Microdata\Argentina\1998, which
is the kind I wanted to identify contains the parent folder name... I altered
to search for the last "\" and see if what was to the left of it was numeric.

By the way, very elegant recursive algorithm!

Best regards and thanks again, you saved my day!

Rafael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top