Loop break continue script

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of bin bin 3 weeks, 4 days ago.

  • Author
    Posts
  • #74044
    Profile photo of bin
    bin
    Participant

    Hello

    I try to create a script with validation input, and condition: if folder exist enter yes to continue the scipt, or quit to return the powershell host command, else if the folder does not exist continue the script.
    the break and continue have the same result it continue the script
    # the scipt
    $folderexist=test-path c:\history
    if ($Folderexist) {
    do{
    $input=read-host "folder exist if you want enter Y(y) to continue or Q(q) to quit"

    if ($input -match "Q"){
    write-host "you choice to quit the script to powershell host"
    break
    }elseif ($input -match "Y"){write-host "continue script"
    }
    }
    until ($input -match "y|q")

    }else{write-host "continue script"}

    #continue script
    .....thé script continuez wish 100 lines

  • #74048
    Profile photo of Sam Boutros
    Sam Boutros
    Participant
    $myPath = 'c:\history'
    
    if (Test-Path $myPath) {
        $Looping = $true
        do {
            $myInput = (Read-Host "Folder ($myPath) exists, hit Y/y to continue or Q/q to stop").ToUpper()
            switch ($myInput) {
                Y { $Looping = $false }
                Q { $Looping = $false }
                Default { [console]::beep(300,300) }
            }
        } while ($Looping)
        if ($myInput -eq 'Y') {
            #region Script branch when folder exists AND Y/y is selected
            "Folder ($myPath) exists, and (Y) is selected"
    
            #endregion
        } else {
            #region Script branch when folder exists AND Q/q is selected
            "Folder ($myPath) exists, and (Q) is selected"
    
            #endregion    
        }
    } else {
        #region Script branch when folder does NOT exist
        "Folder ($myPath) does not exist"
    
        #endregion
    }
    
    • #74093
      Profile photo of bin
      bin
      Participant

      Gréât......Tanks

You must be logged in to reply to this topic.