2016-08-15 119 views
0

我想使用PowerShell查找存在文件的所有祖父目錄,但無法考慮如何執行此操作。我想我需要做這樣的事情:如何查找文件所在的所有Grand-Parent目錄

Get-ChildItem -Filter MyApp.exe -Recurse | format-table Directory 

假設我有以下幾個地方的文件:

C:\test\AppA\201608114\MyApp.exe 
C:\test\AppA\20160812a\MyApp.exe 
C:\test\AppA\201608136\MyApp.exe 
C:\test\AppB\201607115\MyApp.exe 
C:\test\AppB\201607104\MyApp.exe 
C:\test\OtherApps\AppC\201606234\MyApp.exe 

所以在上面的例子中,我想PowerShell來輸出:

C:\test\AppA 
C:\test\AppB 
C:\test\OtherApps\AppC 

有沒有一種方法可以在Powershell中實現這一點。幾乎就在剛剛寫了這一個Perl/Python腳本點,但想我會放棄PowerShell的射擊在它第一次

+0

這些都是祖父母目錄... –

回答

3

簡單地得到文件的目錄的父:

Get-ChildItem -Filter MyApp.exe -Recurse | ForEach-Object { 
    $_.Directory.Parent.FullName 
} | Sort-Object -Unique 
+0

這是密切的,但他們並不明顯 – Denis

+0

@Denis現在他們是。 –

+0

真棒,也只是想出如何讓他們獨特。這不是太糟糕。 – Denis

相關問題