Count sub-sub folders only

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of adi dumitras adi dumitras 2 years, 2 months ago.

  • Author
    Posts
  • #18869
    Profile photo of Tony Wainwright
    Tony Wainwright
    Participant

    Hi

    I have a folder structure such as
    Top
    – Sub
    – Sub
    – – SubSub
    – Sub
    – – SubSub
    – – – SubSubSub
    I need to be able to count just the SubSub folders, ignoring the Sub folders.

    I could count everything the 1st, 2nd and 3rd level sub folders ($sub) and subtract the 1st level ($top) by using:

    $top = (Get-ChildItem -Path "C:\Program Files" | Measure-Object).Count
    $sub = (Get-ChildItem -Path "C:\Program Files" -Recurse | Measure-Object).Count
    $tot =  $sub - $top

    but how would I ignore the 3rd level sub folders?

  • #18873
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    In this case, you wouldn't want to use the -Recurse switch. Try this:

    (Get-ChildItem -Path 'C:\Program Files\*\*' -Directory).Count
    
  • #18876
    Profile photo of adi dumitras
    adi dumitras
    Participant

    I needed something like this once too. I am attaching the script file

You must be logged in to reply to this topic.