2013-10-06 28 views
7

我想在一個路徑來獲得文件(實際上是大量的文件)的列表,遞歸,不包括某些類型:如何從Get-ChildItem中排除項目列表導致powershell?

Get-ChildItem -Path $path -Recurse | ? { $_.Name -notlike "*.cs" -and $_.Name -notlike "*.tt" } 

但我排除的一個長長的清單(僅舉幾例):

Get-ChildItem -Path $path -Recurse | ? { <# what to put here ?#> } 

@("*.cs", "*.tt", "*.xaml", "*.csproj", "*.sln", "*.xml", "*.cmd", "*.txt") 

如何使用這種形式獲得的名單?

回答

9

這工作太:

get-childitem $path -recurse -exclude *.cs,*.tt,*.xaml,*.csproj,*.sln,*.xml,*.cmd,*.txt 
+2

它與接受的答案有何不同? – pinepain

+1

@pinepain,我認爲它更簡單,單行,不太冗長,因此與之前接受的答案有所區別。 – Tar

14

您可以提供排除到Get-ChildItem-exclude參數:

$excluded = @("*.cs", "*.tt", "*.xaml", "*.csproj", "*.sln", "*.xml", "*.cmd", "*.txt") 
get-childitem -path $path -recurse -exclude $excluded 
+0

感謝。這裏的見解是能夠將數組傳遞給-declude – sdjuan

4

這裏是你將如何使用WHERE對象cmdlet做到這一點:

$exclude = @(".cs", ".tt", ".xaml", ".csproj", ".sln", ".xml", ".cmd", ".txt") 
Get-ChildItem -Path $path -Recurse | Where-Object { $exclude -notcontains $_.Extension } 

如果你不想目錄在結果中返回,然後使用:

$exclude = @(".cs", ".tt", ".xaml", ".csproj", ".sln", ".xml", ".cmd", ".txt") 
Get-ChildItem -Path $path -Recurse | Where-Object { (-not $_.PSIsContainer) -and ($exclude -notcontains $_.Extension) } 
1
Set-Location C:\ 

$ExcludedcDirectory = "Windows|Program|Visual|Trend|NVidia|inet" 
$SearchThis = Get-ChildItem -Directory | where Name -NotMatch $ExcludedcDirectory 

$OutlookFiles = foreach ($myDir in $SearchThis) {  
    $Fn = Split-Path $myDir.fullname 
    $mypath = "Get-ChildItem -Path $Fn\*.pst, *.ost -Recurse -ErrorAction SilentlyContinue" 

    Invoke-Expression "$mypath" 
} 
$OutlookFiles.FullName