[英]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?最好的方法是什么?
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.