WhiteHorse
WhiteHorse

Reputation: 63

IF syntax in powershell struggle

Can you guys work it out. I'm pretty sure it's just minor syntax error cause i'm noob at this... I'm always getting "Wrong file" no matter what input($name) is.

$name = "DE123456.zip"
if ($name -contains "DE") 
{
  Get-ChildItem C:\test\$name | 
   % {
     $to  = $_.basename.length - 2
     $path = $_.basename.substring( 0, $to)
     & "C:\test\7z.exe" "x" "-y" $_.fullname "-oC:\test\$path"
     }
  }
ELSE
{
"Wrong file"
}

Upvotes: 1

Views: 68

Answers (2)

Joey
Joey

Reputation: 354516

A syntax error would be emitted before your script ran.

Your problem is that -contains as an operator only checks for existence of values in an array. You want the .Contains method on the string instead:

if ($name.Contains('DE')) ...

Upvotes: 2

CB.
CB.

Reputation: 60918

Change

if ($name -contains "DE") 

with

if ($name -match "DE") 

and:

get-help about_comparison_operators

to know the difference.

Upvotes: 0

Related Questions