Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
}
like image 985
Fitzgery Avatar asked Feb 21 '20 15:02

Fitzgery


4 Answers

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

Some nice, clean examples. Hope this helps.

like image 172
Matthew Avatar answered Oct 21 '22 06:10

Matthew


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"
        }
    }
}
like image 26
Nicicalu Avatar answered Oct 21 '22 06:10

Nicicalu


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
}
like image 38
Mathias R. Jessen Avatar answered Oct 21 '22 06:10

Mathias R. Jessen


A select-string version:

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

yes
like image 30
js2010 Avatar answered Oct 21 '22 04:10

js2010