Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 103
Default List Folders using Excel

Hi All,

I am trying to build a routine that will recursively search through all
folders and subfolders for a given path and list all folders.

I have found exmples of how to list all files but not only the folders.

Any ideas?

TIA

Andy


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 156
Default List Folders using Excel

For that you need the FileSystemObject. try the code below:

CODE

Sub recursiveFolderSearch(ByVal sPath As String)

Dim fs As Object
Dim oTopFolder As Object
Dim oFolder As Object
Dim oSubFolders
Dim oSubFolder

Set fs = VBA.CreateObject("Scripting.Filesystemobject")
Set oTopFolder = fs.GetFolder(sPath)

For Each oFolder In oTopFolder.subfolders
Range("A1").End(xlDown).Value = oFolder.Path
If oFolder.subfolders.Count 0 Then
Call recursiveFolderSearch(oFolder.Path)
End If
Next

End Sub

Sub recursiveList()

Call recursiveFolderSearch("c:\program files)

End Sub
<<<< END CODE <<<<

HTH

Philip

"Andibevan" wrote:

Hi All,

I am trying to build a routine that will recursively search through all
folders and subfolders for a given path and list all folders.

I have found exmples of how to list all files but not only the folders.

Any ideas?

TIA

Andy



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default List Folders using Excel

Andy,

In VBA, go to the Tools menu, choose References, and put a check
next to Microsoft Scripting Runtime. Then, use the following
code:

Sub Start()
Dim FSO As Scripting.FileSystemObject
Dim Rng As Range
Set FSO = New Scripting.FileSystemObject
Set Rng = Range("A1")
Const cSTART_FOLDER = "H:\ExcelProjects" '<<< CHANGE
ListFiles FSO.GetFolder(cSTART_FOLDER), Rng
End Sub

Sub ListFiles(Fldr As Scripting.Folder, Rng As Range)
Dim F As Scripting.File
Dim FF As Scripting.Folder
For Each F In Fldr.Files
Rng.Value = F.Path
Set Rng = Rng(2, 1)
Next F

For Each FF In Fldr.SubFolders
ListFiles FF, Rng
Next FF
End Sub


"Andibevan" wrote in message
...
Hi All,

I am trying to build a routine that will recursively search
through all
folders and subfolders for a given path and list all folders.

I have found exmples of how to list all files but not only the
folders.

Any ideas?

TIA

Andy




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 103
Default List Folders using Excel

Thanks for both the suggestions - works a dream

"Chip Pearson" wrote in message
...
Andy,

In VBA, go to the Tools menu, choose References, and put a check
next to Microsoft Scripting Runtime. Then, use the following
code:

Sub Start()
Dim FSO As Scripting.FileSystemObject
Dim Rng As Range
Set FSO = New Scripting.FileSystemObject
Set Rng = Range("A1")
Const cSTART_FOLDER = "H:\ExcelProjects" '<<< CHANGE
ListFiles FSO.GetFolder(cSTART_FOLDER), Rng
End Sub

Sub ListFiles(Fldr As Scripting.Folder, Rng As Range)
Dim F As Scripting.File
Dim FF As Scripting.Folder
For Each F In Fldr.Files
Rng.Value = F.Path
Set Rng = Rng(2, 1)
Next F

For Each FF In Fldr.SubFolders
ListFiles FF, Rng
Next FF
End Sub


"Andibevan" wrote in message
...
Hi All,

I am trying to build a routine that will recursively search
through all
folders and subfolders for a given path and list all folders.

I have found exmples of how to list all files but not only the
folders.

Any ideas?

TIA

Andy






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
Can I create folders in explorer from an excel list njmcr Excel Discussion (Misc queries) 5 February 28th 13 12:22 PM
Please help to list folders and subfolders tree in Excel or Word observer[_2_] Excel Discussion (Misc queries) 3 March 18th 09 03:42 PM
Can anyone help me Create Excel list of files in windows folders solrac1956 Excel Worksheet Functions 1 November 28th 05 11:07 PM
Creating folders and subfolders from excel file list afaubert Excel Discussion (Misc queries) 4 November 8th 05 11:44 PM
Get folders list Eugene[_5_] Excel Programming 1 December 10th 03 10:44 AM


All times are GMT +1. The time now is 03:02 PM.

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

About Us

"It's about Microsoft Excel"