2013-10-06 9 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、私はそれがよりシンプルで、1つのライナー、あまり冗長ではないと思うので、(以前は)受け入れられた答えと区別されています。 – Tar

14

あなたは-excludeパラメータでGet-ChildItemに除外を供給することができます:あなたがディレクトリに移動したくない場合は

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

:ここ

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

感謝を。ここでの洞察は、配列を渡すことができることです-exclude – sdjuan

4

は、あなたがどこに-Objectコマンドレットを使用してそれを行うだろう方法です結果にも返されます。

$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 
関連する問題