The snippet below detects from a list of files which of them is a Directory on Ftp
as C# it will be like below
var files = new List<string>(){"App_Data", "bin", "Content"};
var line = "drwxr-xr-x 1 ftp ftp 0 Mar 18 22:41 App_Data"
var dir = files.First(x => line.EndsWith(x));
How I can transalte the last line in PowerShell ?
as Robert Groves said, Select-Object -First Occurence do the tricks, you can also use -Last Occurence.
by the way, like any other static .Net method you can use linq in powershell.
Doug Finke produced a great video ( only 7 mins ) about converting C# to Powershell http://dougfinke.com/video/CSharpToPowerShell.html
Roberts example is very good indeed, though comma delimiting will implicitly be treated as an array
the shortest way of doing it would be to put it all into a single pipeline :
This is a really simple implementation for
First
:Instead of returning
$null
, you could throw anInvalidOperationException
exception.Something like this...
These versions of the last line would all accomplish the same:
It was pointed out that the above is not exactly equivalent in behavior to Linq.First because Linq.First throws exceptions in two cases:
If you wanted that behavior exactly, you'd need some extra guard code.