Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Checking for subfolders and creating | Excel Worksheet Functions | |||
Auto look through subfolders | Charts and Charting in Excel | |||
Get list of subfolders | Excel Programming | |||
Modify - look at files within subfolders | Excel Programming | |||
Create subfolders in a folder | Excel Programming |