使用Powershell计数具有特定名称的子文件夹中的文件数 [英] Using powershell to count number of files in subfolder with specific name

查看:75
本文介绍了使用Powershell计数具有特定名称的子文件夹中的文件数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我已经开始处理一个问题,我需要知道某个名称的子文件夹中有多少个文件,该文件在整个目录中重复多次.我要计数的所有文件夹都具有相同的名称.例如:

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

到目前为止,这是我计算所有内容的地方.我需要添加/修改什么才能只查看并计算所需的区域.我不熟悉supershell,并且一直在努力弄清各种问题并将其拼凑起来.

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

推荐答案

我可能会做这样的事情.

I would probably do something like this.

首先获取所有文件夹,然后遍历它们.如果文件夹是"folder_I_want",则获取计数.

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

这篇关于使用Powershell计数具有特定名称的子文件夹中的文件数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆