2017-06-13 61 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