How to build a Dictionary recursively? (1 Viewer)

johnywhy

Registered User.
Local time
Yesterday, 22:41
Joined
Aug 24, 2010
Messages
26
This code actually returns a correct result, but i can tell that it's flawed.
It loads a dictionary with all the files in a directory and all subdirectories.

  • I don't get a chance to `Set dFiles = Nothing`. Is there a way to check if it's the last loop?
  • The initial caller uses `Set dF = Get_Files("C:\Data\Projects\Dev Tools\Updater")`, but the recursion uses `Get_Files oSubFolder.Path`. That's inconsistent. The reason is that the initial caller uses the return value, but the recursion uses the static object. Is there a way to make the recursion use the return value?
  • Should the Dictionary object be passed as an argument?
  • oFSO gets set to Nothing repeatedly during recursion, before i'm done using it, so it gets repeatedly recreated for sub-Directories. That doesn't make sense -- it shouldn't get destroyed until finished using it.

Code:
Function Get_Files(ByVal sDir As String) As Dictionary
          Static dFiles As Dictionary
          If dFiles Is Nothing Then Set dFiles = New Dictionary
       
          Static oFSO As FileSystemObject
          If oFSO Is Nothing Then Set oFSO = New FileSystemObject
       
          Dim oFolder As IWshRuntimeLibrary.Folder
          Set oFolder = oFSO.GetFolder(sDir)
       
          Dim oFile As IWshRuntimeLibrary.File
          For Each oFile In oFolder.Files
                    dFiles.Add oFile.Path, oFile.Name
          Next oFile
       
          Dim oSubFolder As IWshRuntimeLibrary.Folder
          For Each oSubFolder In oFolder.SubFolders
                    Get_Files oSubFolder.Path 'recursion
          Next oSubFolder
       
          Set oFSO = Nothing
          Set Get_Files = dFiles
End Function


Sub Test()
          Dim dF As Dictionary
          Set dF = Get_Files("C:\Data\")
          Debug.Print dF.Keys(2)
End Sub
 
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 22:41
Joined
Oct 29, 2018
Messages
21,358
Hi. Just curious, what is your recursive function supposed to do?
 

johnywhy

Registered User.
Local time
Yesterday, 22:41
Joined
Aug 24, 2010
Messages
26
Hi. Just curious, what is your recursive function supposed to do?
It loads a dictionary with all the files in a directory and all subdirectories.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 22:41
Joined
Oct 29, 2018
Messages
21,358
It loads a dictionary with all the files in a directory and all subdirectories.
And did you really need to use a dictionary object? Also, is the function working as expected, and you're only asking if there's a better way?
 

johnywhy

Registered User.
Local time
Yesterday, 22:41
Joined
Aug 24, 2010
Messages
26
This one is a little better, because it's called the same way every time, and it's not necessary to set the local dictionary to nothing, since it's passed as a parameter.

But, i don't get to Set oFSO = Nothing

Code:
Function Get_Files2(ByVal sDir As String, dFiles As Dictionary, Optional oFSO As FileSystemObject) As Boolean
          If oFSO Is Nothing Then Set oFSO = New FileSystemObject
          
          Dim oFolder As IWshRuntimeLibrary.Folder
          Set oFolder = oFSO.GetFolder(sDir)
          
          Dim oFile As IWshRuntimeLibrary.File
          For Each oFile In oFolder.Files
                    dFiles.Add oFile.Path, oFile.Name
          Next oFile
          
          Dim oSubFolder As IWshRuntimeLibrary.Folder
          For Each oSubFolder In oFolder.SubFolders
                    Get_Files2 oSubFolder.Path, dFiles, oFSO   'recursion
          Next oSubFolder
          
          Set oFolder = Nothing
End Function
 

johnywhy

Registered User.
Local time
Yesterday, 22:41
Joined
Aug 24, 2010
Messages
26
And did you really need to use a dictionary object? Also, is the function working as expected, and you're only asking if there's a better way?
For this question, i'm asking about the Dictionary object. Of course it could be done with an Array or Collection, but I don't want to use an Array just because i don't know how to do it with a Dictionary. i want to learn how to do it with a Dictionary.

As mentioned in the OP, the code returns a correct result, but i can tell that it's flawed.
 

johnywhy

Registered User.
Local time
Yesterday, 22:41
Joined
Aug 24, 2010
Messages
26
Here's another version. In this case, my FileSystemObject and Dictionary aren't set to Nothing until the very end, and no objects are left open.

Problem is: it's two functions. Would be nicer if could do it in one function.

Code:
Function Get_Files3(ByVal sDir As String) As Dictionary
          Dim dFiles As Dictionary
          Set dFiles = New Dictionary
          
          Dim oFSO As FileSystemObject
          Set oFSO = New FileSystemObject

          Get_Files3_sub sDir, dFiles, oFSO
          
          Set oFSO = Nothing
          
          Set Get_Files3 = dFiles
          Set dFiles = Nothing
End Function


Function Get_Files3_sub(sDir As String, dFiles As Dictionary, oFSO As FileSystemObject) As Boolean
          Dim oFolder As IWshRuntimeLibrary.Folder
          Set oFolder = oFSO.GetFolder(sDir)
          
          Dim oFile As IWshRuntimeLibrary.File
          For Each oFile In oFolder.Files
                    dFiles.Add oFile.Path, oFile.Name
          Next oFile
          
          Dim oSubFolder As IWshRuntimeLibrary.Folder
          For Each oSubFolder In oFolder.SubFolders
                    Get_Files3_sub oSubFolder.Path, dFiles, oFSO     'recursion
          Next oSubFolder
          
          Set oFolder = Nothing
End Function
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 22:41
Joined
Oct 29, 2018
Messages
21,358
For this question, i'm asking about the Dictionary object. Of course it could be done with an Array or Collection, but I don't want to use an Array just because i don't know how to do it with a Dictionary. i want to learn how to do it with a Dictionary.

As mentioned in the OP, the code returns a correct result, but i can tell that it's flawed.
Hi. I don't know what you plan to do with the dictionary, but I only asked my question because I was curious if this demo could have helped with whatever it was you were trying to accomplish. Could you please give it a look and let me know if it would have been useful to you or not? Thank you.

 

moke123

AWF VIP
Local time
Today, 01:41
Joined
Jan 11, 2013
Messages
3,852
Where are you using it? In a form?
Dim the dictionary object in the declarations.
 

johnywhy

Registered User.
Local time
Yesterday, 22:41
Joined
Aug 24, 2010
Messages
26
Hi. I don't know what you plan to do with the dictionary, but I only asked my question because I was curious if this demo could have helped with whatever it was you were trying to accomplish. Could you please give it a look and let me know if it would have been useful to you or not? Thank you.

Thx, this is helpful. It's similar to what i'm already doing. i'll go with my last version, above.
 

johnywhy

Registered User.
Local time
Yesterday, 22:41
Joined
Aug 24, 2010
Messages
26
Where are you using it? In a form?
Dim the dictionary object in the declarations.
Yes, if this procedure lived in a class or module by itself, then the Dictionary could be a module-level variable. However, it shares a module with various unrelated procedures. My last version, above, is my favorite so far.
 

Users who are viewing this thread

Top Bottom