I am having difficulty wrapping my head around how I would construct a dictionary of arrays.
What I’m wanting to do is have a recursive method that goes through a folder structure and appends a path to a folderitem when it finds a folderitem with a duplicate name.
The dictionary’s key is a filename. The value would be an array of strings of the paths to every folderitem in the folder structure with the same name.
Right now I have a method that counts the number of folderitems with the same name in the folder structure that looks like this:
for each f as FolderItem in folder.children
if f.IsFolder then
if not f.IsFolder then
if not g_FilenameDictionary.HasKey(f.name) then
filenameDictionary.Value(f.name) = 1
filenameDictionary.value(f.name) = filenameDictionary.Value(f.name) + 1
And of course that works, to count and keep track of the number of duplicate filenames in the folder structure.
However I’m having difficult understanding how I would create another dictionary one that has as its keys a file name, and as its values an array of the
f.NativePath of the folderitems (f).
Phillip one point to note that if you can’t guarantee that your “Filenames” will be unique then using that as your key will lead you to trouble fast.
@Mike Cotrone ,
I believe that’s sort of the point, in as much as I want to keep track of NativePaths to each folderitem that happens to have a particular filename. That is to say, if a filename occurs more than one time in the folder structure I want to build a dictionary that has an array of all of the paths that are associated with that particular filename.
Like the method I have that has, as its keys, filenames and increments a counter indicating how many times a particular filename occurs in the folder structure.
(Please forgive me if I’m not understanding what you’re saying.)
It might be cleaner if you use a class and a class array structure to store you class (each folderitem entry). Perhaps something like this (not tested as its off the top of my head). It just seems not natural to keep managing that key process for minimal gain IMO. Plus Im sure you may need more than just the f.name perhaps?
Also you could use your matching code and then use an array off of the parent class to add all of the nativePaths that is associated to it.
Create Base Class:
folderItemInfoClass with properties such as:
myFolderNameStr as String
myFolderDisplayNameStr as String
isFolderBool as Boolean
myIndexIdInt as Integer
myFolderNativePathStr as String
myAssociatedNativePathStrArr() as String
// SETUP CLASS ARRAY
Var folderItemInfoClassArr() as folderItemInfoClass (You should stash this as a global variable in a module or someplace so you don't lose it when this method goes out of scope)
// LOOP EACH FOLDER AND BUILD YOUR CLASSES
for each f as FolderItem in folder.children
// CREATE CLASS FOR THIS SPECIFIC ENTRY
Var thisFolderItemInfoClass as New folderItemInfoClass
thisFolderItemInfoClass.folderNameStr = f.name
thisFolderItemInfoClass.myFolderDisplayNameStr = f.Displayname
// ADD CODE TO DO YOUR MATCHING TO CATCH THE DUP NAMES ADD ADD THEM TO THE
thisFolderItemInfoClass.myAssociatedNativePathStrArr.AddRow("Whatever this Is")
// ADD THIS CLASS TO YOUR CLASS STORAGE ARRAY
Im sure this isn’t exact but hopefully shows you maybe another flexible route.
Thanks for the ideas @Mike Cotrone , I’ll have a closer look at your code snippets in the morning. That might well be useful to me.
One thing I was hoping to to achieve is an understanding of how to implement a dictionary of arrays in a recursive method, like the example I posted above.
I’ll look at your suggestions more closely tomorrow morning.
In basically the same way: Check whether the key already exists, use AddRow to add the new pathname to the array stored under that key, or, if it doesnt, store a new array with the just found pathname as its sole element.
Having said that, I would add the FolderItem itself rather than a pathname. Pathnames are generally to be avoided.
@Michael Hußmann, thanks.
I figured out how to solve my issue.
Point taken but regarding using the FolderItem but in the case of the utility I’m making the native path is actually what I need to report, plus they need to be grouped and sorted in such a way that it seemed easier not to extract that information from FolderItems later to accomplish that.