Question:
I need to be able to list all of the directories in a root folder that do not contain a file which contains the string merged
. So for example, if I run the script from the directory C:\out
, I will get a list of all the folders inside of C:\out
which do not match the wildcard merged
.
Example:
Tree:
1 2 3 4 5 6 7 8 9 10 11 12 |
-c:\out --c:\out\test123 ---sample.meta ---sample_merged.meta --c:\out\test456 ---sample.meta ---sample_merged.meta --c:\out\test789 ---sample.meta --c:\out\test013 ---sample.meta |
Output:
1 2 3 |
test789 test013 |
Answer:
You can use Get-ChildItem
, Where-Object
, and Select-String
:
1 2 3 4 |
Get-ChildItem -Directory | Where-Object { -not (Get-ChildItem $_ -File -Recurse -Filter *merged*) } | Select-Object Name, LastWriteTime |
Below is an explanation of what each line does:
- Get all directories in the current directory.
- Set up a filter that searches recursively through each directory for file names that contain the string
merged
. Only pass on the directory if none are found. - Get the
Name
andLastWriteTime
attributes of each directory.
Note too that you can write it more concisely if you want:
1 2 |
gci -dir | ? { !(gci $_ -file -recur -filter *merged*) } | select name, *w*time |