ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Enumerate all subfolders? (https://www.excelbanter.com/excel-programming/446389-enumerate-all-subfolders.html)

Robert Crandal[_2_]

Enumerate all subfolders?
 
My target folder is "C:\UserFolders". I would like to run a
search of this directory and obtain the names of all subfolders.
Im only searching for folder names, not filenames. Also, this
search should only go one level...I'm only interested in
obtaining the names of the folders that exist in the target
folder "C:\UserFolders".

Can someone show me how to do this? It would be nice if
I could store all the folder names in an array of strings. Or,
it would also be nice if I could just enumerate through
each folder string one at a time using a single string variable.

Thank you!



isabelle

Enumerate all subfolders?
 
hi Robert,

Sub List_SubFolders()
Dim fd As FileDialog, rw As Integer, sfd
Dim oFolder As String, fso As Object

'================================================= ============
'you can replace this part by oFolder = "C:\UserFolders"

Set fd = Application.FileDialog(msoFileDialogFolderPicker)
With fd
.AllowMultiSelect = False
.Show
oFolder = .SelectedItems(1)
End With
'================================================= ============

Set fso = CreateObject("Scripting.FileSystemObject")
Set sfoFolder = fso.getfolder(oFolder)

For Each sfd In sfoFolder.SubFolders
rw = rw + 1
Range("A" & rw) = Right(sfd, Len(sfd) - Len(sfoFolder) - 1)
Next
End Sub


--
isabelle



Le 2012-06-20 22:16, Robert Crandal a écrit :
My target folder is "C:\UserFolders". I would like to run a
search of this directory and obtain the names of all subfolders.
Im only searching for folder names, not filenames. Also, this
search should only go one level...I'm only interested in
obtaining the names of the folders that exist in the target
folder "C:\UserFolders".

Can someone show me how to do this? It would be nice if
I could store all the folder names in an array of strings. Or,
it would also be nice if I could just enumerate through
each folder string one at a time using a single string variable.

Thank you!



isabelle

Enumerate all subfolders?
 
also for a single string variable,

Sub List_SubFolders()
Dim list()
Dim fd As FileDialog, fso As Object
Dim oFolder As String, n As Integer, i As Integer, sfd


Set fd = Application.FileDialog(msoFileDialogFolderPicker)
With fd
.AllowMultiSelect = False
.Show
oFolder = .SelectedItems(1)
End With

Set fso = CreateObject("Scripting.FileSystemObject")
Set sfoFolder = fso.getfolder(oFolder)

For Each sfd In sfoFolder.SubFolders
ReDim Preserve list(n)
list(n) = string_variable & Right(sfd, Len(sfd) - Len(sfoFolder) - 1)
n = n + 1
Next

'test
For i = LBound(list) To UBound(list)
Debug.Print list(i)
Next

End Sub



--
isabelle



Le 2012-06-21 00:05, isabelle a écrit :
hi Robert,

Sub List_SubFolders()
Dim fd As FileDialog, rw As Integer, sfd
Dim oFolder As String, fso As Object

'================================================= ============
'you can replace this part by oFolder = "C:\UserFolders"

Set fd = Application.FileDialog(msoFileDialogFolderPicker)
With fd
.AllowMultiSelect = False
.Show
oFolder = .SelectedItems(1)
End With
'================================================= ============

Set fso = CreateObject("Scripting.FileSystemObject")
Set sfoFolder = fso.getfolder(oFolder)

For Each sfd In sfoFolder.SubFolders
rw = rw + 1
Range("A" & rw) = Right(sfd, Len(sfd) - Len(sfoFolder) - 1)
Next
End Sub



Robert Crandal[_2_]

Enumerate all subfolders?
 
Well thank you, that was very helpful! 8)


"isabelle" wrote in message ...
hi Robert,

Sub List_SubFolders()
Dim fd As FileDialog, rw As Integer, sfd
Dim oFolder As String, fso As Object

'================================================= ============
'you can replace this part by oFolder = "C:\UserFolders"

Set fd = Application.FileDialog(msoFileDialogFolderPicker)
With fd
.AllowMultiSelect = False
.Show
oFolder = .SelectedItems(1)
End With
'================================================= ============

Set fso = CreateObject("Scripting.FileSystemObject")
Set sfoFolder = fso.getfolder(oFolder)

For Each sfd In sfoFolder.SubFolders
rw = rw + 1
Range("A" & rw) = Right(sfd, Len(sfd) - Len(sfoFolder) - 1)
Next
End Sub


--
isabelle




Robert Crandal[_2_]

Enumerate all subfolders?
 
One more question Isabelle.....

What is the data type for the "sfd" variable??
Is it an integer?? It's just sitting by itself on the line
where "rw" is defined as Integer. I've never seen that
type of variable definition before.

Thanks!


"isabelle" wrote in message ...
hi Robert,

Sub List_SubFolders()
Dim fd As FileDialog, rw As Integer, sfd
Dim oFolder As String, fso As Object

'================================================= ============
'you can replace this part by oFolder = "C:\UserFolders"

Set fd = Application.FileDialog(msoFileDialogFolderPicker)
With fd
.AllowMultiSelect = False
.Show
oFolder = .SelectedItems(1)
End With
'================================================= ============

Set fso = CreateObject("Scripting.FileSystemObject")
Set sfoFolder = fso.getfolder(oFolder)

For Each sfd In sfoFolder.SubFolders
rw = rw + 1
Range("A" & rw) = Right(sfd, Len(sfd) - Len(sfoFolder) - 1)
Next
End Sub


--



isabelle

Enumerate all subfolders?
 
hi Robert,

Dim sfoFolder As Object, sfd As Object

--
isabelle



Le 2012-06-21 02:12, Robert Crandal a écrit :
One more question Isabelle.....

What is the data type for the "sfd" variable??
Is it an integer?? It's just sitting by itself on the line
where "rw" is defined as Integer. I've never seen that
type of variable definition before.

Thanks!


"isabelle" wrote in message ...
hi Robert,

Sub List_SubFolders()
Dim fd As FileDialog, rw As Integer, sfd
Dim oFolder As String, fso As Object

'================================================= ============
'you can replace this part by oFolder = "C:\UserFolders"

Set fd = Application.FileDialog(msoFileDialogFolderPicker)
With fd
.AllowMultiSelect = False
.Show
oFolder = .SelectedItems(1)
End With
'================================================= ============

Set fso = CreateObject("Scripting.FileSystemObject")
Set sfoFolder = fso.getfolder(oFolder)

For Each sfd In sfoFolder.SubFolders
rw = rw + 1
Range("A" & rw) = Right(sfd, Len(sfd) - Len(sfoFolder) - 1)
Next
End Sub


--



GS[_2_]

Enumerate all subfolders?
 
In a For Each loop you can use Variant type or Object type. In
Isabelle's case, the type for variable 'sfd' defaults to Variant
because it was not declared explicitly. That causes some extra
processing so VB can determine/evaluate a type and so I prefer to
explicitly declare type to save the extra work on the part of VB. No
big deal for the odd use but if there's a lot of untyped vars it adds
up in procedure processing time.

In context of its use here I'd go with...

Dim oSubFldr As Object
OR
Dim sf As Variant

...so the code is better self documented as to 'type' and description. I
reserve something like 'sfd' for use as a counter because there's
nothing there to intuitively tell me its type. In Isabelle's example it
serves that purpose even though it's not actually counting, because it
is iterating a collection. Personally, I'd go with 'sf' because the 'd'
part doesn't associate to anything in my naming convention schema, but
that's just me. ('S'ub'F'olders!)

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion




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

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