Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
148 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...