I would like to find all directories at the top level from the location of the script that are stored in subversion.
In C# it would be something like this
Directory.GetDirectories(".")
.Where(d=>Directories.GetDirectories(d)
.Any(x => x == "_svn" || ".svn"));
I'm having a bit of difficulty finding the equivalent of "Any()" in PowerShell, and I don't want to go through the awkwardness of calling the extension method.
So far I've got this:
Get-ChildItem | ? {$_.PsIsContainer} | Get-ChildItem -force | ? {$_.PsIsContainer -and $_.Name -eq "_svn" -or $_.Name -eq ".svn"
This finds me the svn
directories themselves, but not their parent directories - which is what I want. Bonus points if you can tell me why adding
| Select-Object {$_.Directory}
to the end of that command list simply displays a sequence of blank lines.
A variation on @JaredPar's answer, to incorporate the test in the
Test-Any
filter:Now I can write "any" tests like
I recommend the following solution:
You can tighten this up a bit:
Note - passing $__.Name to the nested gci is unnecessary. Passing it $_ is sufficent.
Unfortunately there is no equivalent in PowerShell. I wrote a blog post about this with a suggestion for a general purpose Test-Any function / filter.
Blog post: Is there anything in that pipeline?
You can use the original LINQ
Any
:I think that the best answer here is the function proposed by @JaredPar, but if you like one-liners as I do I'd like to propose following
Any
one-liner:%{ $match = $false }{ $match = $match -or YOUR_CONDITION }{ $match }
checks that at least one item match condition.One note - usually the Any operation evaluates the array until it finds the first item matching the condition. But this code evaluates all items.
Just to mention, you can easily adjust it to become
All
one-liner:%{ $match = $false }{ $match = $match -and YOUR_CONDITION }{ $match }
checks that all items match condition.Notice, that to check Any you need
-or
and to check All you need-and
.