Question:
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
$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!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$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" } |
Answer:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$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)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$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.