簡體   English   中英

PowerShell:計算給定名稱目錄和子文件夾中的文件

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

我需要計算給定文件夾中的文件

例子:

Folder A
Folder B
Folder C

對於每個文件夾,我需要計算有多少文件,最后計算總數。

最好的方法是什么?

嘗試這個:

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

在此處輸入圖像描述

標記到@Esperento57: ,有用的答案。

是的,它確實有效,但如果文件夾路徑沒有文件,它也會出錯; 而在混合中使用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
#>

而使用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