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


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


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


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



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


--




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


--


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


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
Enumerate DSNs XP Excel Programming 2 May 22nd 07 06:47 PM
enumerate sheet Philippe L. Balmanno Excel Worksheet Functions 4 January 4th 06 12:41 AM
copy subfolders, replace text in files and save files in copied subfolders pieros Excel Programming 0 November 1st 05 12:08 PM
how to enumerate? tendercare New Users to Excel 3 August 1st 05 06:15 PM
Enumerate form items Kurt[_7_] Excel Programming 4 June 28th 05 10:26 PM


All times are GMT +1. The time now is 08:51 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"