MSH: Get-ChildItem Needs a Type parameter

Discussion in 'Scripting' started by Keith Hill, Nov 30, 2005.

  1. Keith Hill

    Keith Hill Guest

    Lately I've been doing a lot of commands where I have to test MshIsContainer
    because I either want to operate on a directory (container) or a file (leaf)
    e.g.:

    gci . -rec | foreach { if (!$_.MshIsContainer){$_.IsReadOnly = $false}}

    It might be nice if get-childitem had a -type parameter, just like
    test-path's type parameter, that allows you to filter the output to only
    output that specific type. Then my command simplifies to:

    gci . -rec -type Leaf | foreach {$_.IsReadOnly = $false}

    IMO, this is directly analogous to *ix's find command e.g::

    find . -type d # find all dirs under current dir
    find . -type f # find all files under current dir
     
    Keith Hill, Nov 30, 2005
    #1
    1. Advertisements

  2. Thank you. We will keep your suggestion in mind going forward.
    A simple function can be written to get the leaves:
    function get-leaf {param($path) dir -rec $path |
    foreach{if(!$_.MshIsContainer) {$_}}}

    gci . -rec -type Leaf | foreach {$_.IsReadOnly = $false}

    can be written as

    get-leaf . | foreach {$_.IsReadOnly = $false}
     
    Kevin Loo [MSFT], Nov 30, 2005
    #2
    1. Advertisements

  3. we've talked about this a bit in the past - we thought that this was more
    filtering operation - so it boiled down to this:

    gci . -rec | where { ! $_.MshIsContainer } | foreach { $_.IsReadonly =
    $false }

    but, I understand what you're looking for. The question is whether the
    current filtering capability should be expanded or not. I'm not sure if
    Leaf/Container is enough - you might want to look for more than that. By
    extending the dynamic parameter, each provider could have the behavior that
    makes the most sense.

    jim
     
    James Truher [MSFT], Dec 1, 2005
    #3
  4. I usually wind up filtering with where:

    gci . -rec | where { ! $_.MshIsContainer } | ...

    An issue that I see with providing a "type" parameter for the core cmdlets,
    is that not all providers will necessarily have a leaf/container (think
    variable/function drive). However, dynamic parameters reflects the
    functionality of the underlying resource - perhaps this could be done in
    that way - I'm not sure whether this has been filed as a feature request -
    please do so

    Thanks

    jim
     
    James Truher [MSFT], Dec 1, 2005
    #4
  5. Keith Hill

    Keith Hill Guest

    Yeah I filed an enhancement request a few days ago - 219377051. I need to
    try using the -filter option. I haven't yet. I could see where it might
    make sense to do this with a filter. However since filter is just a
    "string", the discoverability suffers as well as potential intellisense
    support in the future. Hmm.
     
    Keith Hill, Dec 1, 2005
    #5
  6. Keith Hill

    Keith Hill Guest

    Do you mean filtering as in where-object {} or are you talking about
    the -filter parameter that is on a number of cmdlets? I can't seem to find
    any docs on the -filter parameter in order to figure out how it's used.
     
    Keith Hill, Dec 2, 2005
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.