How to process parent-child data structures

The “Repeat” action introduced in v4.5 makes it easier to deal with parent-child (i.e. hierarchical) data processing.

The example below demonstrates how the action is used to scan a folder with all its subfolders (no matter how deep the folder structure is) in order to find a folder path with the maximum depth. Folders and subfolders is a classic example of a parent-child data structure.

This is how the example works:

Step 1) Have a list of folders.

Step 2) For that list produce a list of direct subfolders, i.e. folders that are located exactly in the folders in step 1, but not any deeper.

Step 3) Go to step 1 with the list of subfolders (which becomes the new list of folders), repeat until the list of subfolders becomes empty.

Step 4) Append all lists of folders into one.

Step 5) Find the deepest folder path.

The algorithm starts with the folder path specified by the parameter. Note that the iterated module uses the “Input” action. So in the 1st iteration of the “Repeat” action that “Input” is populated with the input dataset of the “Repeat” action which is the parameter value (i.e. the folder name). So in the 1st iteration we obtain the 1st level children for the parent folder.

However, in the 2nd iteration of the “Repeat” action, “Input” is populated with the result table of the previous iteration. This is the unique behavior of the “Repeat” action in the “UNTIL” mode. When the iterated module has the “Input” action, it’s populated with the result of the previous iteration (except for the very 1st iteration when it’s populated with the input dataset of the “Repeat” action itself). So in the 2st iteration we obtain the children of children, i.e. the 2nd level children.

And so on until we reach a folder that don’t have any subfolders. Since the “Repeat” action works in the “Append all results” mode, it will concatenate all non-empty results of iterations and produce a list of all subfolder paths for the initially provided root folder.

scan-folders.morph (7.7 KB)

PS. Technically speaking, for the purpose of finding the deepest folder path it would suffice to run the “Repeat” action in the “Return the last non-empty result” because with every iteration we go one level deeper. So the last iteration would naturally have the subfolders with deepest path.