"Bob Landau" <BobLandau@xxxxxx> wrote in message
news:8AB72523-4133-4912-AAC5-CA4D2A109F41@xxxxxx
....
Quote:
> Here is another example using Get-ChildItem; say I'd like to find all
> files
> that have their archive and read-only bit set and order these by Creation
> time and then by name. For CMDs "dir" its done this way
>
> dir c:\junk /aar-d /o-dn /tc
>
> however to do the same thing for GCI you need a longer line even if you do
> use the shortcut aliases
>
> gci "c:\junk" | % { $attr = $( $_.get_Attributes() ); if ( -not ( (
> $attr
> -bxor 33 ) -band 33 ) -and -not ( $attr -band 16 ) ) { $_} } | Sort
> @{e={$_.CreationTime}; Descending=$true}, @{e={$_.Name}; Ascending=$true}
>
> 33 is ReadOnly | Achive and 16 is directory I used the values rather than
> the constants to shorten up the line.
>
> In both of these cases most people would prefer to use simpler, older
> method.
Not to disqualify your general comment, but you should be aware of how you
can simplify the above command:
dir c:\junk | where {($_.attributes -band 33) -eq 33} | sort
@{e={$_.creationtime};desc=$true}, name
1) "where {<condition>}" provides the same logic as "foreach {if
(<condition>) {$_}}" and is much shorter.
2) You don't need to explicitly filter out directories since filtering on
Archive and Read only already rules them out.
3) As a result of 1 and 2, you only have to access the Attributes property
once therefore you don't need to use a temporary variable.
4) Sort order is ascending by default on all properties regardless of the
other properties' settings, so you don't need to be explicit on properties
that you want to sort this way.
Hope that helps,
Jacques