2016-11-15 658 views
1

我在这里有点卡住了。基本上我有一个脚本从文本文件中读取目录和文件路径。使用Get-Content从文本文件获取过滤的内容

我想驱动器号排除路径像program filesC:\Windows目录来进行筛选。每个驱动器号都有一个变量但是,当我使用下面的代码段时,它似乎没有过滤任何东西。如果包含其中包含CEG似乎并不重要。它总是将文件的内容整合到每个变量中。

有什么建议吗?

$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:用户名\音乐 C:\ Users \ $ env:用户名\视频 –

回答

1

尝试这样

解决方案1:

$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:

$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

你的权利马特,我已经修改该ty;) – Esperento57

0

您需要使用-like和-notlike。包含可用于检查,如果事情是在数组中,而不是字符串

+0

但它会检查输入$ _的内容,是不是那种数组?如果我错了,请纠正我的错误! :) –

+0

不,这是一个对象,你是“工作”对该对象的属性的字符串表示形式 – 4c74356b41

+0

好的谢谢澄清!还在学习PowerShell的功能。 –

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

没有。如果不包含'程序文件',那么不包含'C:\ Program Files文件(x86)' – Esperento57

+0

因此,一个文件夹'C:\ myApp \ native Program Files \'将被排除,这可能不是他想要的 –