Fitzgery
Fitzgery

Reputation: 718

PowerShell If Statements: IF equals multiple specific Texts

Good Morning! I'm working on a script and I'm trying to include an IF/Else statement based off a text variable with multiple specific text options and to test for a directory path that will be named after the variable and make the directory if it doesn't exist. So example would be

$text = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title,"")

If($text -match "Specific Text1") -and (!(Test-Path $Path\$text))){

new-item -ItemType directory -path $Path\$text

}
ElseIF($text -match "Specific Text2") -and (!(Test-Path $Path\$text))){

new-item -ItemType directory -path $Path\$text

}

ElseIF($text -match "Specific Text3") -and (!(Test-Path $Path\$text))){

new-item -ItemType directory -path $Path\$text

}

ElseIF($text -notmatch "Specific Text1","Specific Text2","Specific Text3"){
write-host "invalid input"
}

Im providing users a list of the valid inputs, that can be entered into the text box. When I try running the script I'm still getting errors saying the folder already exists and it's not ignoring it like it should be.

A side question is there a cleaner way to write this?

Edit

Below is the answer that worked perfect for me. Thank you everyone for your responses!

$text = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title,"")
$AcceptableText = @("Specific Text1","Specific Text2","Specific Text3")
If ($text -in $AcceptableText)
{
    If (!(Test-Path $Path\$text)) 
    {
        new-item -ItemType directory -path $Path\$text
    }
}
Else
{
    write-host "invalid input"
}

Upvotes: 7

Views: 6953

Answers (4)

Matthew
Matthew

Reputation: 1166

Looks like you're trying to create the directories if your user chooses one of 3 text phrases and the directory doesn't already exist, and complain to your user if they choose something other than the 3 text phrases. I would treat each of those cases separately:

$text = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title,"")
$AcceptableText = @("Specific Text1","Specific Text2","Specific Text3")
If ($text -in $AcceptableText)
{
    If (!(Test-Path $Path\$text)) 
    {
        new-item -ItemType directory -path $Path\$text
    }
}
Else
{
    write-host "invalid input"
}

Or you could test for the existence of the directory first like this:

$text = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title,"")
$AcceptableText = @("Specific Text1","Specific Text2","Specific Text3")
If (!(Test-Path $Path\$text))
{
    If (($text -in $AcceptableText)) 
    {
        new-item -ItemType directory -path $Path\$text
    }
    Else
    {
        write-host "invalid input"
    }
}

Edit

Or, if you want to be tricky and avoid the Test-Path (as suggested by @tommymaynard), you can use the following code (and even eliminate the Try|Catch wrappers if you don't want error checking)

$text = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title,"")
$AcceptableText = @("Specific Text1","Specific Text2","Specific Text3")
If (($text -in $AcceptableText)) 
{
    try { mkdir -path $Path\$text -ErrorAction Stop } #Change to -ErrorAction Ignore if you remove Try|Catch
    catch [System.IO.IOException] { } #Do nothing if directory exists
    catch { Write-Error $_ }        
}
Else
{
    write-host "invalid input"
}

Edit

Also worth noting that there are many ways to Use PowerShell to Create Folders.

Upvotes: 8

js2010
js2010

Reputation: 27491

A select-string version:

if ('text' | select-string text,text2,text3) { 'yes' } 

yes

Upvotes: 1

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174690

Start by worrying about the input validation separately from the Test-Path call:

$validValues = 'Specific text 1','Specific text 2','Specific text 3'
$text = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title,"")

if($validValues -notcontains $text) {
    Write-Host "Invalid output"
}
elseif(!(Test-Path -LiteralPath $path\$text)) {
    Write-Host "Item exists"
}
else {
    New-Item -ItemType Directory -LiteralPath $Path\$text
}

Upvotes: 4

Nicicalu
Nicicalu

Reputation: 795

Yes you can :) Note that in the switch statement, default is being executed when all the other cases are not matched. Please test it and let me know.

$text = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title,"")
if (!(Test-Path $Path\$text))
{
    switch ($text)
    {
        "Specific Text1"
        {
            new-item -ItemType directory -path $Path\$text
        }
        "Specific Text2"
        {
            new-item -ItemType directory -path $Path\$text
        }
        "Specific Text3"
        {
            new-item -ItemType directory -path $Path\$text
        }
        default
        {
            write-host "invalid input"
        }
    }
}

Upvotes: 5

Related Questions