Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Macro to enumerate subdirs and files in a directory!

Is there anyone know a Macro to enumerate subdirs and files in a
directory!
Thanks,


---
Message posted from http://www.ExcelForum.com/

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Macro to enumerate subdirs and files in a directory!

This is not my code. I cannot remeber who authored it - PSC maybe.

Private Sub Path()
On Error GoTo handle
Dim fso, fs, f, x
Set fso = CreateObject("scripting.filesystemobject") 'create
filesystemobject
Set fs = fso.getfolder("c:\") 'gets a hanlde on th
path
' cange as appropriate
Set f = fs.subfolders 'gets a list of al
the folders in the path
Set x = fs.files 'gets a list of al
the files in the path

For Each w In f 'for..next loop
'w this point is the folders in the directory
'for every folder in the list, add it to the subfolder
listbox

' your code here

Next 'loop

For Each v In x
'v lists the files in each folder

' your code here

Next
Exit Sub
handle:
msg = MsgBox("An error has been found. Make sure that the driv
and path exist.", vbCritical, "Error")
End Su

--
Message posted from http://www.ExcelForum.com

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,236
Default Macro to enumerate subdirs and files in a directory!

Bookworm98,


Here's one I put together a while ago - it could do with some optimising,
but I was using it for a once-off task so wasn't fussy.


Sub test()
Dim arr() As String, i As Long

arr = RecursiveDir("C:\WINDOWS\")

For i = LBound(arr) To UBound(arr)
Debug.Print arr(i)
Next
End Sub

Function RecursiveDir(Path As String, Optional Attributes As
VbFileAttribute) As String()
Dim strPath As String, strResult As String, i As Long, j As Long, k As
Long
Dim arrPath() As String, arrFile() As String, arrTemp() As String

On Error Resume Next

strPath = Path
If Right(strPath, 1) < Application.PathSeparator Then strPath = strPath
& Application.PathSeparator

strResult = Dir(strPath, vbDirectory Or Attributes)
Do Until strResult = ""
If GetAttr(strPath & strResult) And vbDirectory Then
If Not (strResult = "." Or strResult = "..") Then
i = UBound(arrPath) + 1
If CBool(Err.Number) Then
Err.Clear: i = 0
End If
ReDim Preserve arrPath(i)
arrPath(i) = strPath & strResult & Application.PathSeparator
End If
Else
i = UBound(arrFile) + 1
If CBool(Err.Number) Then
Err.Clear: i = 0
End If
ReDim Preserve arrFile(i)
arrFile(i) = strPath & strResult
End If
strResult = Dir
Loop

i = LBound(arrPath)
If Not CBool(Err.Number) Then
For i = LBound(arrPath) To UBound(arrPath)
arrTemp = RecursiveDir(arrPath(i), Attributes)
j = LBound(arrTemp)
If Not CBool(Err.Number) Then
For j = LBound(arrTemp) To UBound(arrTemp)
k = UBound(arrFile) + 1
If CBool(Err.Number) Then
Err.Clear: k = 0
End If
ReDim Preserve arrFile(k)
arrFile(k) = arrTemp(j)
Next
Else
Err.Clear
End If
Next
Else
Err.Clear
End If

arrTemp = arrPath

i = LBound(arrFile)
If Not CBool(Err.Number) Then
For i = LBound(arrFile) To UBound(arrFile)
j = UBound(arrTemp) + 1
If CBool(Err.Number) Then
Err.Clear: j = 0
End If
ReDim Preserve arrTemp(j)
arrTemp(j) = arrFile(i)
Next
Else
Err.Clear
End If

RecursiveDir = arrTemp
End Function


Rob


"bookworm98 " wrote in message
...
Is there anyone know a Macro to enumerate subdirs and files in a
directory!
Thanks,


---
Message posted from http://www.ExcelForum.com/



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Macro to enumerate subdirs and files in a directory!

I get it!
Thank you very much Keiran. Good day!


---
Message posted from http://www.ExcelForum.com/

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
Open files in the same directory houghi Excel Discussion (Misc queries) 2 May 21st 07 03:56 PM
Macro - Open all word files in a directory Dileep Chandran Excel Worksheet Functions 11 December 19th 06 02:12 PM
Files in a directory? Greg B Excel Discussion (Misc queries) 5 May 11th 05 09:46 PM
Check if directory empty OR no of files in directory. Michael Beckinsale Excel Programming 2 December 4th 03 10:12 PM
run macro for all files in the directory igor Excel Programming 3 July 17th 03 03:48 PM


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