返回网络路径中的文件数量? (Powershell的)

时间:2017-06-13 18:30:33

标签: powershell powershell-v4.0

我正在尝试在网络路径中获取不同数量的文件,但由于某种原因,它返回的数量超过了。

文件:

20170612PA
20170612PB
20170611PA
20170611PB

脚本:

$numberOfDistinctfiles = Get-ChildItem *.txt -Path $filePath |
    Select-Object { $_.BaseName.Substring(0,8) } |
    Get-Unique -AsString |
    Measure-Object |
    ForEach-Object { $_.Count }

这应该返回2但是由于某种原因它返回3.我已经尝试删除并将文件放在另一个网络路径中它仍然返回相同的。我的剧本有什么问题吗?

$numberOfDistinctfiles = Get-ChildItem *.txt -Path $filePath |
    Group-Object { $_.BaseName.Substring(0,8) } |
    Measure-Object |
    ForEach-Object { $_.Count }

2 个答案:

答案 0 :(得分:1)

[已在How do I get the number of distinct files in a directory in powershell?中回答。重新发布,直到它作为副本关闭]

使用Group-Object。例如:

Get-ChildItem *.txt | Group-Object { $_.Name.Substring(0,8) }

答案 1 :(得分:0)

试试这个

 $numberOfDistinctfiles =(Get-ChildItem  $filePath  -Filter "*.txt" -file | Select { $_.BaseName.Substring(0, [system.Math]::Min($_.BaseName.length, 8)) } -Unique).Count