Using powershell to count number of files in subfolder with specific name

夙愿已清 提交于 2021-02-08 09:47:07

问题


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

回答1:


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"
    }
}



回答2:


somthing like this?

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


来源:https://stackoverflow.com/questions/40050145/using-powershell-to-count-number-of-files-in-subfolder-with-specific-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!