ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to find specific subfolders (https://www.excelbanter.com/excel-programming/337024-how-find-specific-subfolders.html)

Rafael Guerreiro Osorio

How to find specific subfolders
 
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




Bob Phillips[_6_]

How to find specific subfolders
 
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)


"Rafael Guerreiro Osorio"
wrote in message ...
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






Rafael Guerreiro Osorio

How to find specific subfolders
 
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


"Bob Phillips" wrote:

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)


"Rafael Guerreiro Osorio"
wrote in message ...
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







Bob Phillips[_6_]

How to find specific subfolders
 
Excellent.

Recursion is neat isn't it?

Regards

Bob


"Rafael Guerreiro Osorio"
wrote in message ...
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


"Bob Phillips" wrote:

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)


"Rafael Guerreiro Osorio"


wrote in message

...
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










All times are GMT +1. The time now is 09:26 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com