[MSH] get-childitem - quick way to find/sort on number children

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

  1. Keith Hill

    Keith Hill Guest

    The only thing I'm seeing is GetFileSystemInfos and calling that method and
    getting its Length seems rather clunky. What I want to do is sort on and/or
    display the number of child items in a container.
     
    Keith Hill, Nov 16, 2005
    #1
    1. Advertisements

  2. get-childitem . -recurse | group-object MshParentPath | sort-object Count
     
    Jeff Jones [MSFT], Nov 16, 2005
    #2
    1. Advertisements

  3. Keith Hill

    Keith Hill Guest

    I was kind of expecting an "Items" property for each MshContainer such that
    I could easily do a:

    get-childitem . | foreach { if ($_.MshIsContainer) { $_.Items.Count }}

    Most container objects expose their child item collections - at least in
    ..NET they do.
     
    Keith Hill, Nov 17, 2005
    #3
  4. It depends on what the provider implementation returns. In the FileSystem
    provider the returned object for a container is a DirectoryInfo object. It
    has a method called GetFileSystemInfos() which returns an array of its
    children. So if you know you are in the file system you can do:

    MSH > get-childitem . | foreach { if ($_ -is [System.IO.DirectoryInfo]) {
    $_.GetFileSystemInfos().Length } }

    or if you are in the registry you can use the SubKeyCount property:

    MSH > get-childitem . | foreach { if ($_ -is [Microsoft.Win32.RegistryKey])
    { $_.SubKeyCount } }

    You could even put the type in a switch statement and handle all the ones
    you know about. My solution below will work for any provider regardless of
    the type of object it returns.

    --
    Jeff Jones [MSFT]
    Microsoft Command Shell Development
    Microsoft Corporation
    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Jeff Jones [MSFT], Nov 17, 2005
    #4
  5. Keith Hill

    Keith Hill Guest

    Yeah I would like to see a general solution to this problem. The one
    problem with your solution is that I only want to see the number of
    child-items one level deep - no further. Your solution walks them all and
    in my case the dir structure is deep. It would be wicked cool if you could
    do this:

    get-childitem . -recurse 1 | group-object MshParentPath | sort-object Count

    Notice the "1"? It would be nice if -recurse were an int parameter that
    defaulted to a sentinal value like -1 or 0 which means recurse all levels
    then you could tell gci how many levels you want it to recurse.
     
    Keith Hill, Nov 19, 2005
    #5
  6. Keith Hill

    Keith Hill Guest

    get-childitem . -recurse 1 | group-object MshParentPath |
    sort-object Count
    I decided to submit this: 132291840
     
    Keith Hill, Nov 19, 2005
    #6
  7. You can use wildcard matching for that:

    MSH > get-childitem *\* | group-object MshParentPath | sort-object Count
     
    Jeff Jones [MSFT], Nov 21, 2005
    #7
  8. Keith Hill

    Keith Hill Guest

    Another excellent tip. Thanks.
     
    Keith Hill, Nov 21, 2005
    #8
    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.