2

So I've started working on a problem where I need to know how many files are in a subfolder of a certain name, that is repeated multiple times in throughout the directory. All folders I want to count have the same name. For example:

Main Folder
    Subfolder
        Folder I want to count
        Folder A
        Folder B
    Subfolder
        Folder I want to count
        Folder C
        Folder D

I'm able to count the number of files in all subfolders recursively, but I don't know how to only look at folders named " Folder I want to count ".

This is where I've gotten so far to count everything. What do I need to add/modify to only look at and count in the area I want. I'm not familiar with supershell, and have been working to make sense of various questions and cobble this together.

Get-ChildItem -recurse | Where {!$_.PSIsContainer} | Group Directory | Format-Table Name, Count -autosize
3
  • Do the "folder I want to count" all have the same name, or any other similarities? Commented Oct 14, 2016 at 18:53
  • All the folders I want to count have the same name. Sorry, I'll edit and clarify that. Commented Oct 14, 2016 at 18:53
  • Get-childitem -filter Directoryname -Directory -Recurse Commented Oct 14, 2016 at 19:06

2 Answers 2

2

I would probably do something like this.

First get all the folders, then run through them. And if the folder is the "folder_I_want", get the count.

$folders = Get-ChildItem C:\Users\David\Documents\SAPIEN\test -Recurse | ?{ $_.PSIsContainer } | select name, fullname

foreach ($folder in $folders)
{
    #Write-Host "$folder"
    if ($folder.name -eq "folder_I_want")
    {
        $fullname = $folder.fullname
        #Gets the count of the files in "Folder I want". It will filter out folders.
        $count = (Get-ChildItem $fullname | where { $_.PSIsContainer -EQ $false }).count
        Write-Host "The amount of files in the folder I want: $count"
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, this is really close to what I need! What would I add in the output line to report the full directory path?
For example: "The amount of files in the [FULL DIRECTORY PATH] is $count" Sorry for the double comment. Still new to SO.
It's already defined as $fullname. So this would work: write-host "The amount of files in the path $fullname is $count"
Thanks! This is what I needed!
2

somthing like this?

(gci -Path c:\temp\ -Recurse -File | where fullname -like "*\yourname\*").Count

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.