Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27
Default 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



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default 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





  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27
Default 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






  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default 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








Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Checking for subfolders and creating Gizmo63 Excel Worksheet Functions 1 August 16th 06 12:46 AM
Auto look through subfolders grewpp Charts and Charting in Excel 1 February 14th 06 02:35 PM
Get list of subfolders Darren Hill[_3_] Excel Programming 3 March 6th 05 09:28 PM
Modify - look at files within subfolders Steph[_3_] Excel Programming 1 October 8th 04 06:51 PM
Create subfolders in a folder Myrna Rodriguez Excel Programming 2 July 15th 04 04:10 PM


All times are GMT +1. The time now is 03:21 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"