Home > Mobile >  Comparing file names to an array of file masks in Powershell
Comparing file names to an array of file masks in Powershell

Time:04-14

I'm looking for the best way take a file, and test to see if it's filename contains an element in an array.

For example, the file name AB_011_Test.txt, and the list @("010", "011", "012"), I need a test to see if the file name contains one of the elements of the list. Obviously in this example it would return true, but the file XZ_999_Test.txt would return false.

TIA!

CodePudding user response:

You can join all your array elements with | to create a pattern that can match either of the elements and use it along with -match operator operator.

PS > "AB_011_Test.txt" -match (@("010", "011", "012") -Join "|")
True
PS > "XZ_999_Test.txt" -match (@("010", "011", "012") -Join "|")
False
  • Related