使用两个 Where 命令运行两个检查 [英] Using two Where commands to run two checks

查看:36
本文介绍了使用两个 Where 命令运行两个检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这个命令

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

但也需要用到这个命令

Where {$_.FullName -notlike $IgnoreDirectories}

我应该使用 &&或 II 中的

Should I use a && or II as in

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

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

?

我想要完成的是提取每个 zip 和 rar 文件,但我想跳过在我的某些目录中提取 zip 或 rar 文件.对此的最佳解决方案是什么?

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?

推荐答案

如有疑问,请阅读 文档.

Windows PowerShell 支持以下逻辑运算符.

Windows PowerShell supports the following logical operators.

  • -and 逻辑与.仅当两个语句都为 TRUE 时才为 TRUE.
  • -or 逻辑或.当其中一个或两个语句为 TRUE 时为 TRUE.
  • -xor 逻辑异或.仅当其中一个语句为 TRUE 而另一个语句为 FALSE 时才为 TRUE.
  • -not 逻辑非.否定它后面的语句.
  • ! 逻辑上不是.否定它后面的语句.(同 -not)
  • -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)

将两个子句放在同一个脚本块中,并用适当的逻辑运算符将它们连接起来.对于两个子句之间的逻辑 AND 使用 -and:

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
}

对于两个子句之间的逻辑 OR 使用 -or:

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.

请注意,由于两个 | 之间的空字符串,您的正则表达式 zip||rar 匹配任何扩展名.要仅匹配扩展名为 .rar.zip 的项目,请删除一个管道:$_.Extension -match "zip|rar".

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".

这篇关于使用两个 Where 命令运行两个检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆