2016-11-12 47 views
-1

我使用這個命令使用兩個在哪裏命令運行兩個檢查

Where {$_.Extension -match "zip||rar"} 

但需要使用這個命令太

Where {$_.FullName -notlike $IgnoreDirectories} 

我應該使用& &或II作爲

Where {$_.Extension -match "zip||rar"} && Where {$_.FullName -notlike $IgnoreDirectories} 

Where {$_.Extension -match "zip||rar"} || Where {$_.FullName -notlike $IgnoreDirectories} 

我想要完成的是讓每個zip和rar文件都被提取出來,但是我想跳過在我的一些目錄中提取zip或rar文件。什麼是最好的解決方案?

回答

1

如有疑問,請閱讀documentation

Windows PowerShell支持以下邏輯運算符。

  • -and邏輯和。只有當兩個語句都爲TRUE時才爲TRUE。
  • -or邏輯或。當其中一個或兩個語句均爲TRUE時爲TRUE。
  • -xor邏輯上的排他或。僅當其中一個語句爲TRUE而另一個語句爲FALSE時爲TRUE。
  • -not邏輯不是。否定隨後的陳述。
  • !邏輯不是。否定隨後的陳述。 (同-not

把兩個子句在同一個腳本塊,並將它們與相應的邏輯運算符連接。對於邏輯之間的兩個子句使用-and

Where-Object { 
    $_.Extension -match "zip||rar" -and 
    $_.FullName -notlike $IgnoreDirectories 
} 

對於邏輯之間或者兩個子句使用-or

Where-Object { 
    $_.Extension -match "zip||rar" -or 
    $_.FullName -notlike $IgnoreDirectories 
} 

在你的情況下,它可能是前者。


請注意,你的正則表達式zip||rar任何擴展名匹配由於兩個|之間的空字符串。要僅匹配擴展名爲.rar.zip的項目,請刪除一個管道:$_.Extension -match "zip|rar"

+0

您的建議不起作用,因爲我沒有收到錯誤「您必須在表達式或語句中的意外標記'.FullName'之後提供值表達式。」我該如何解決這個錯誤?我認爲這就是爲什麼我需要兩個單獨的地方。 – Underdog

+0

這似乎解決了表達式問題,但它會工作嗎?其中{($ _。Extension -match「zip || prd」) - 和($ .FullName -notlike $ ignore)} – Underdog

+0

這應該是'$ _',而不是'$'。不知道下劃線如何失蹤。固定。 –