简体   繁体   English

PowerShell:计算给定名称目录和子文件夹中的文件

[英]PowerShell: Count files in a given name directory and subfolders

I need to count files inside given folders我需要计算给定文件夹中的文件

example:例子:

Folder A
Folder B
Folder C

For each folder I need to count how many files there are, and at the end sum the total.对于每个文件夹,我需要计算有多少文件,最后计算总数。

What is the best to way to do this?最好的方法是什么?

try this:尝试这个:

$DirList=@('C:\temp\BATCHHISTOCRE', 'C:\temp\tmp', 'C:\temp\444')

$DirList | %{

    [pscustomobject]@{
    Dirname=$_
    NbFile=(Get-ChildItem -Path $_ -File).Count # Add -recurse if you want all tree into your dir
     }
}

在此处输入图像描述

Tagging on to @Esperento57: , helpful answer.标记到@Esperento57: ,有用的答案。

Yeppers, it does work, but it will also error off if the folder path has no files;是的,它确实有效,但如果文件夹路径没有文件,它也会出错; whereas using Measure-Object in the mix, can address that.而在混合中使用Measure-Object可以解决这个问题。

$DirList = @('D:\Temp\AddressFiles',
'D:\Temp\BonoboGitServer',
'D:\Temp\Book'
)

$DirList | 
ForEach-Object {
    [pscustomobject]@{
        Dirname = $PSItem
        NbFile  = (Get-ChildItem -Path $PSItem -File).Count
    }
}
# Results
<#
Dirname              NbFile
-------              ------
D:\Temp\AddressFiles      3
The property 'Count' cannot be found on this object. Verify that the property exists.
At line:8 char:5
+     [pscustomobject]@{
+     ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], PropertyNotFoundException
    + FullyQualifiedErrorId : PropertyNotFoundStrict

D:\Temp\Book              4
#>

Whereas using the Measure-Object cmdlet, addresses the issue...而使用Measure-Object cmdlet 可以解决问题......

$DirList = @('D:\Temp\AddressFiles',
'D:\Temp\BonoboGitServer',
'D:\Temp\Book'
)

$DirList | 
ForEach-Object {
    [pscustomobject]@{
        Dirname = $PSItem
        NbFile  = (Get-ChildItem -Path $PSItem -File | 
                  Measure-Object).Count
    }
}
# Results
<#

Dirname                 NbFile
-------                 ------
D:\Temp\AddressFiles         3
D:\Temp\BonoboGitServer      0
D:\Temp\Book                 4
#>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM