Loops, queues, and stacks
The file system is an example of a tree-like structure that can be traversed by making use of a loop and a queue or stack.
Consider the following directory tree:
Project
| - A
| | - B
| | - C
| | - Large
| | - Tree
| - D
| - Large
| - Tree
The Get-ChildItem -Recurse
command may be used to find items in this path and all child paths.
However, if there was a requirement to avoid looking inside folders named Large
, then the Recurse
parameter is not particularly efficient. Any filtering would have to happen after the command had run, and the command would still visit those directories.
The snippet below may be used to create this directory tree, allowing the loop below to be tested. The folders are created in the current working directory:
New-Item Project\A\B\C -ItemType Directory
New-Item Project\A\B\Large\Tree -ItemType Directory
New-Item Project\D\Large\Tree -ItemType Directory
To...