MS Scripting for directory tree parsing

OaklandJim

New Member
Joined
Nov 29, 2018
Messages
28
Office Version
2019
Platform
Windows
Go easy on me. I'm more of a decent hacker using other's code not a programmer. 1st post here.

Tried using recursive code I found based on MS scripting (which I barely understand) to parse directory tree to fill an array with all paths. It seems to be parsing fine as I can tell HOW MANY dirs it found. But I cannot figure out how to use the scripting object to get/stash the paths' NAME. It is close but when I try to use objFile.Path to get the path my sub chokes on it.

In caller I set up objects
' Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'
' Get the top folder
Set objTopFolder = objFSO.GetFolder(sAnchorPath)

Then this sub chokes when I try to access the path names during parsing.

Sub RecursiveTreePaths( _
objFolder As Scripting.Folder, _
ByRef pasDirs() As String)


' scripting object variables
Dim objFile As Scripting.file
Dim objSubFolder As Scripting.Folder

' NEXT array element
Dim iUboundNext As Integer

iUboundNext = UBound(pasDirs) + 1


' Debug.Print "iUboundNext = " & iUboundNext
'FAILS Debug.Print "objFile.Path = " & objFile.Path

' Add another element to the byref param array
' for the directory currently being recorded.
ReDim Preserve pasDirs(iUboundNext)


' put current path into array
'FAILS pasDirs(iUboundNext) = objFile.Path


' recursive parsing of directory tree
For Each objSubFolder In objFolder.SubFolders
Call RecursiveTreePaths(objSubFolder, pasDirs)
Next objSubFolder


End Sub
 

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
5,949
You're using objFile, which is declared but not defined. Use objFolder instead:
Code:
    pasDirs(iUboundNext) = objFolder.Path
 

OaklandJim

New Member
Joined
Nov 29, 2018
Messages
28
Office Version
2019
Platform
Windows
As usual, dumb *** move. My half-assed hacking resulted in me leaving an orphan script object for FILES -- because original code looped files too. I just want DIRECTORY tree. Apoligies: Will use
Code:
next time.
 

OaklandJim

New Member
Joined
Nov 29, 2018
Messages
28
Office Version
2019
Platform
Windows
Thanks to John_w for the help! It was spot on. Previous post should've said "...will use code tags next time." Learning...
 

Forum statistics

Threads
1,078,151
Messages
5,338,534
Members
399,241
Latest member
mattyoxford

Some videos you may like

This Week's Hot Topics

Top