Using two Where commands to run two checks

谁说胖子不能爱 提交于 2019-12-13 08:35:53

问题


I am using this command

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

but need to use this command too

Where {$_.FullName -notlike $IgnoreDirectories}

Should I use a && or II as in

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

or

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

?

What I am trying to accomplish is to have every zip and rar file be extracted but I want to skip the extraction of the zip or rar files in some of my directories. What is the best solution for this?


回答1:


When in doubt, read the documentation.

Windows PowerShell supports the following logical operators.

  • -and Logical and. TRUE only when both statements are TRUE.
  • -or Logical or. TRUE when either or both statements are TRUE.
  • -xor Logical exclusive or. TRUE only when one of the statements is TRUE and the other is FALSE.
  • -not Logical not. Negates the statement that follows it.
  • ! Logical not. Negates the statement that follows it. (Same as -not)

Put both clauses in the same scriptblock and connect them with the appropriate logical operator. For a logical AND between the two clauses use -and:

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

For a logical OR between the two clauses use -or:

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

In your case it's probably the former.


Note that your regular expression zip||rar matches any extension due to the empty string between the two |. To match only items with the extension .rar or .zip remove one pipe: $_.Extension -match "zip|rar".



来源:https://stackoverflow.com/questions/40567314/using-two-where-commands-to-run-two-checks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!