2013-10-06 24 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