2017-06-12 23 views
4

我目前有這個代碼,它返回的文件的總數,但我不想在這個數字計數多個文件?如何獲取PowerShell中某個目錄中不同文件的數量?

可以說我有這樣的:

01Red.txt 
01Blue.txt 
02Red.txt 
05Red.txt 
05Green.txt 

Get-ChildItem -File *.txt -Path "C:\Users\Test\Desktop\TestDirectory" | Measure-Object | %{$_.Count} 

我想返回基於01,02,05,但我的代碼爲3的總計數,我得到5

我怎樣才能得到它返回3並忽略字符串中前2個字符的所有內容?

回答

7

我可能會建議Group-Object

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

添加| Measure-Object來算(這將是你的榜樣3)分組的數目。

5
Get-ChildItem -File *.txt -Path "C:\Users\Test\Desktop\TestDirectory" 
| select {$_.BaseName.Substring(0,2)} | Get-Unique -AsString | measure 
相關問題