MS Scripting for directory tree parsing

OaklandJim

Well-known Member
Joined
Nov 29, 2018
Messages
833
Office Version
  1. 365
Platform
  1. 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
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
You're using objFile, which is declared but not defined. Use objFolder instead:
Code:
    pasDirs(iUboundNext) = objFolder.Path
 
Upvote 0
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.
 
Upvote 0
Thanks to John_w for the help! It was spot on. Previous post should've said "...will use code tags next time." Learning...
 
Upvote 0

Forum statistics

Threads
1,215,019
Messages
6,122,707
Members
449,093
Latest member
Mnur

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top