2017-06-13 48 views
-1

我想获得网络路径中不同数量的文件,但由于某种原因,它返回1比以上还多。返回网络路径中的文件数量? (Powershell)

文件:

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 } 
+0

@wOxxOm,不知道为什么,但我仍然越来越3.感谢收拾这件事。 – user3266638

+1

可能的重复[如何获取PowerShell中某个目录中不同文件的数量?](https://stackoverflow.com/questions/44504720/how-do-i-get-the-number-of-distinct- files-in-a-directory-in-powershell) –

+0

使用'Group-Object'(就像我在你的其他问题中回答的那样)。 –

回答

0

试试这个

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