2016-11-15 12 views
1

私はここでちょっと立ち往生しています。基本的に私は、テキストファイルからディレクトリとファイルパスを読み込むスクリプトを持っています。Get-Contentでテキストファイルからフィルタリングされたコンテンツを取得する

は私がドライブ文字program filesようパスとC:\Windowsディレクトリを除外することによって、それらをフィルタリングします。各ドライブレターの変数があります。しかし、私が以下のコードを使用すると、何もフィルタリングしていないようです。 が含まれていれば、その中にCEまたはGがあるかどうかは関係ありません。常にの内容をすべての変数にSaveMe.txtというファイルに入れます。

アドバイスはありますか?

$CDrive = Get-Content $PSScriptRoot\SaveMe.txt | where { $_.length -gt 4 -and ($_.Contains("C:\")) -and $_ -NotContains "Program Files" -or "C:\Program Files (x86)" -or "C:\Windows" } 
$EDrive = Get-Content $PSScriptRoot\SaveMe.txt | where { $_.length -gt 4 -and ($_.Contains("E:\")) -and $_ -NotContains "Program Files" -or "C:\Program Files (x86)" -or "C:\Windows" } 
$GDrive = Get-Content $PSScriptRoot\SaveMe.txt | where { $_.length -gt 4 -and ($_.Contains("G:\")) -and $_ -notcontains "Program Files" -or "C:\Program Files (x86)" -or "C:\Windows" } 
+0

はどのようにあなたの '$ PSScriptRoot \ SaveMe.txt'のように見えるのですか? – Avshalom

+0

には、C:¥Users¥$ env:username¥Documents C:¥Users¥$ env:username¥Pictures C:¥Users¥$ env:username¥Desktop C:¥Users¥$ env:usernameのようなフルパスが含まれています。 \お気に入り C:\ Users \ $ env:username \ Music C:\ Users \ $ env:username \ Videos –

答えて

1

この

ソリューション1と同様にしてみてください、ではない文字列をチェックするためである

$CDrive = Get-Content $PSScriptRoot\SaveMe.txt | where { $_.length -gt 4 -and ($_.Contains("C:\")) -and $_ -Notlike "*Program Files*" -and $_ -Notlike "*C:\Windows*" } 
$EDrive = Get-Content $PSScriptRoot\SaveMe.txt | where { $_.length -gt 4 -and ($_.Contains("E:\")) -and $_ -Notlike "*Program Files*" -and $_ -Notlike "*C:\Windows*" } 
$GDrive = Get-Content $PSScriptRoot\SaveMe.txt | where { $_.length -gt 4 -and ($_.Contains("G:\")) -and $_ -Notlike "*Program Files*" -and $_ -Notlike "*C:\Windows*" } 
+0

これは['-contains'演算子](https://technet.microsoft.com/en-us/library/hh847759.aspx?f=255&MSPPError=-2147217396)の仕組みではありません。 – Matt

+0

あなたの '正しいMatt、私はそのtyを変更しました;) – Esperento57

0

-likeと-notlikeを使用する必要があります。

$CDrive = Get-Content $PSScriptRoot\SaveMe.txt | where { $_.length -gt 4 -and $_.Contains("C:\") -and !$_.Contains("Program Files") -and !$_.Contains("C:\Windows") } 
$EDrive = Get-Content $PSScriptRoot\SaveMe.txt | where { $_.length -gt 4 -and $_.Contains("E:\") -and !$_.Contains("Program Files") -and !$_.Contains("C:\Windows") } 
$GDrive = Get-Content $PSScriptRoot\SaveMe.txt | where { $_.length -gt 4 -and $_.Contains("G:\") -and !$_.Contains("Program Files") -and !$_.Contains("C:\Windows") } 

解決方法2:何かが配列である場合に含まれてい

+0

しかし、$ _にパイプされたコンテンツをチェックします。私が間違っているなら、私を修正してください! :) –

+0

いいえ、それはオブジェクトであり、あなたはそのオブジェクトのプロパティの文字列表現に対して「働いています」 – 4c74356b41

+0

説明をありがとう!まだパワーシェルロープを学ぶ。 –

0

述べたように、あなたがすべき組み込みの比較関数-like-notlikeを使用してください。また、比較機能を再度指定せずに-or文を結合することはできません。

しかし、私ははこれが読みやすくなると思い:

$CDrive = Get-Content $PSScriptRoot\SaveMe.txt | where { $_ -match 'C:\\(?!Windows\\|Program Files\\|Program Files \(x86\)\\).+'} 
$EDrive = Get-Content $PSScriptRoot\SaveMe.txt | where { $_ -match 'E:\\(?!Windows\\|Program Files\\|Program Files \(x86\)\\).+'} 
$GDrive = Get-Content $PSScriptRoot\SaveMe.txt | where { $_ -match 'G:\\(?!Windows\\|Program Files\\|Program Files \(x86\)\\).+'} 
0

最適化されたソリューション(ファイルが開いているだけで1回)

[email protected]() 
[email protected]() 
[email protected]() 

Get-Content $PSScriptRoot\SaveMe.txt | where { $_.length -gt 4 -and $_ -notlike "*Program Files*" -and $_ -notlike "*C:\Windows*" } | %{ if ($_.Contains("C:\")) {$CDrive+=$_};if ($_.Contains("E:\")) {$EDrive+=$_};if ($_.Contains("G:\")) {$GDrive+=$_};} 
+0

C:\ Program Files(x86)がありません。また、 'Program Files'を含むフォルダも除外されます。 –

+0

番号。 'Program Files'が含まれていない場合は 'C:\ Program Files(x86)'を含まない – Esperento57

+0

したがって、C:\ myApp \ native Program Files \ 'フォルダは除外されますが、これはおそらく彼が望むものではありません –

関連する問題