I wasn't aware directories would never have the archive bit set but they can
have read, hidden and system (c:\windows\assembly is one of many directories
other attributes set.
At least one reason that it's convoluted is to keep compatibility with how
"CMD /C DIR" processes attribues. This will not work.
($_.Attributes -band 33) -eq 33
because DIR only cares whether these attributes are set or not. For instance
if all I want is the Readonly/System files in the Windows directory it will
return any files that have these bits set. Any other attribute can also be
set and it will still be consider a match so equality can't be used.
There may be a simpler solution than mine. I agree it does seem more complex
than it needs to be but the only way I know of is to
1) $_.attributes XOR 33 (remove all the bits that I'm interested in)
2) AND the above result with 33 (the result will only be truie if the
"interesting" bits weren't set on $_)
3) lastly reverse the logic. The result will be true only as long as the
bits which I'm interested are set; any other bits are ignored.
I suppose I could have just used $_Attributes twice rather than the temp
$var which would have meant I could then use WHERE. But my code is written
now and I want to get this up on the scripting site as is.
thx
bob
"Jacques Barathon [MS]" wrote:
Quote:
> "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
>
>