[MSH] get-childitem and file attributes...

5 views
Skip to first unread message

Mark Rochotte

unread,
Jan 30, 2006, 11:57:40 AM1/30/06
to
Hi all,

I would like to get a list of just directories from the get-childitem cmdlet.
I just can't quite figure this one out.

Any help would be appreciated.

thanks,

m


/\/\o\/\/ [MVP]

unread,
Jan 30, 2006, 12:35:49 PM1/30/06
to

2 way could be :

ls | where {$_.MshIsContainer -eq $true}

or

(gi .).GetDirectories()

(will also show hidden)

gr /\/\o\/\/

/\/\o\/\/ [MVP]

unread,
Jan 30, 2006, 12:47:24 PM1/30/06
to

Addition,

this will let you search on more attributes as Directory (IsContainer)
but MshIsContainer is not provider specific and this is :

ls | where {$_.Attributes -contains "Directory"}

but it may be more clear if you use to on the FS provider.

/\/\o\/\/ [MVP]

unread,
Jan 30, 2006, 1:06:05 PM1/30/06
to

Sorry to keep posting, but last example was wrong,
as the Contains is done on one string, as it gets casted.
so will only show directories with no other attributes, as it is not an
array.

MSH>(gi .).Attributes | gm


TypeName: System.IO.FileAttributes

Name MemberType Definition
---- ---------- ----------
CompareTo Method System.Int32 CompareTo(Object target)
Equals Method System.Boolean Equals(Object obj)
GetHashCode Method System.Int32 GetHashCode()
GetType Method System.Type GetType()
GetTypeCode Method System.TypeCode GetTypeCode()
ToString Method System.String ToString(), System.String
ToString(String format, IFormatProvider provider), System.String ToString...
value__ Property System.Int32 value__ {get;set;}


this way to do it is OK, but not very readable.

MSH>ls | where {$_.Attributes -band 16}

Keith Hill [MVP]

unread,
Jan 30, 2006, 1:24:56 PM1/30/06
to
"/\/\o\/\/ [MVP]" <n...@Spam.mow> wrote in message
news:uaNEhQc...@TK2MSFTNGP12.phx.gbl...

> Mark Rochotte wrote:
>> Hi all,
>>
>> I would like to get a list of just directories from the get-childitem
>> cmdlet. I just can't quite figure this one out.

> ls | where {$_.MshIsContainer -eq $true}

I'm hoping that perhaps for V2 get-childitem will support a -Type property
(like new-item supports) that will get child items of a certain type e.g.:

gci -Type Directory

BTW the above can be simplified to:

ls | where {$_.MshIsContainer}


--
Keith


James Truher [MSFT]

unread,
Jan 30, 2006, 1:45:29 PM1/30/06
to
the equality statement isn't strictly necessary:

gci -force | where { $_.mshiscontainer }

will get directories (even hidden ones)

jim

--
James W. Truher [MSFT]
Monad Program Management
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.

"/\/\o\/\/ [MVP]" <n...@Spam.mow> wrote in message
news:uaNEhQc...@TK2MSFTNGP12.phx.gbl...

Mark Rochotte

unread,
Jan 30, 2006, 2:16:25 PM1/30/06
to
Thank you very much. That will make my life much easier.

m

MSH>>

> TypeName: System.IO.FileAttributes
>
> Name MemberType Definition
> ---- ---------- ----------
> CompareTo Method System.Int32 CompareTo(Object target)
> Equals Method System.Boolean Equals(Object obj)
> GetHashCode Method System.Int32 GetHashCode()
> GetType Method System.Type GetType()
> GetTypeCode Method System.TypeCode GetTypeCode()
> ToString Method System.String ToString(), System.String
> ToString(String format, IFormatProvider provider), System.String
> ToString...
> value__ Property System.Int32 value__ {get;set;}
> this way to do it is OK, but not very readable.
>
MSH>> ls | where {$_.Attributes -band 16}

MSH>>

Mark Rochotte

unread,
Jan 30, 2006, 2:17:07 PM1/30/06
to
Thanks for all the replies.

Now if I could get this level of support in some other ng's! :)

m


> the equality statement isn't strictly necessary:
>
> gci -force | where { $_.mshiscontainer }
>
> will get directories (even hidden ones)
>
> jim
>

/\/\o\/\/ [MVP]

unread,
Jan 30, 2006, 2:29:30 PM1/30/06
to
Mark Rochotte wrote:
> Thanks for all the replies.
np

> Now if I could get this level of support in some other ng's! :)
>

if you ask this question in the DOS NG you mean ;-)

but for the CMD lovers, this shortcut could be handy (just forget to
type the space ;-))

MSH>function dir/ad {gci -force | where { $_.mshiscontainer }}
MSH>dir/ad

Keith Hill [MVP]

unread,
Jan 30, 2006, 3:59:29 PM1/30/06
to
"/\/\o\/\/ [MVP]" <n...@Spam.mow> wrote in message
news:%23nP7CQd...@TK2MSFTNGP12.phx.gbl...

> but for the CMD lovers, this shortcut could be handy (just forget to type
> the space ;-))
>
> MSH>function dir/ad {gci -force | where { $_.mshiscontainer }}
> MSH>dir/ad

Well let me throw in my favorite MSH dir functions :-)

function dir { get-childitem $args -force | sort @{e={$_.MshIsContainer};
asc=$false},@{e={$_.Name}; asc=$true} }
function dirw { get-childitem $args -force | sort @{e={$_.MshIsContainer};
asc=$false},@{e={$_.Name}; asc=$true} | format-wide }
function dirs { get-childitem $args -force | sort Length }
function dirt { get-childitem $args -force | sort LastWriteTime }
function dird { get-childitem $args -force | where { $_.MshIsContainer } }

--
Keith


/\/\o\/\/ [MVP]

unread,
Jan 30, 2006, 4:11:56 PM1/30/06
to

Lol,
to make the party compleet,

let's post the script I just made for my blog after this thread, (still
got to post it)
it Make dir work the DOS way with attributes you can add other switches
also.

so now you can do

dir /ah
dir /ar
dir /ard

etc. and even one more as the CMD one ;-)

you should remove the alias DIR first

del variable:dir

gr /\/\o\/\/

# Dir Function replacement with attributefilter
# /\/\o\/\/ 2006
# http://mow001.blogspot.com

function dir {
if($args.length -ne 0) {
$Script:NewArgs = @()
$Script:filters = @()
foreach ($arg in $args) {
Switch ($arg) {
{$_ -match '-a'} {
if ($_ -match 'r'){$Script:filters += ' | where {
$_.attributes -match "ReadOnly"}'}
if ($_ -match 'd'){$Script:filters += ' | where {
$_.attributes -match "Directory"}'}
if ($_ -match 'h'){$Script:filters += ' | where {
$_.attributes -match "Hidden"}'}
if ($_ -match 's'){$Script:filters += ' | where {
$_.attributes -match "System"}'}
if ($_.remove(0,2) -match 'a'){$Script:filters += ' | where {
$_.attributes -match "Archive"}'}
if ($_ -match 'n'){$Script:filters += ' | where {
$_.attributes -match "NotContentIndexed"}'}
}
{$_ -eq '-d'} {$Script:filters += ' | where { $_.mshiscontainer}'}
default {$Script:NewArgs += $_}
}
}
invoke-command("gci $Script:NewArgs $Script:filters")
} else {
gci
}
}

Reply all
Reply to author
Forward
0 new messages