-->

  • User Input Yes No Prompt in Batch Scripting

      

    User Input Yes No Prompt in Batch Scripting

     

    User Input Yes/No Prompt in Batch Scripting


    Many Batch Scripts require user confirmation before performing an action. For example, a script may ask whether you want to continue, delete a file, or restart a system.

    In such cases, we can use a **Yes/No Prompt** to collect user input and execute different actions based on the response.
    In this tutorial, we'll learn how to create a simple Yes/No prompt in Batch Scripting.



    Example Script

      
      
    @echo off
    
    :start
    cls
    set /p user_input=Do you want to continue? (yes/no)?:
    if not defined user_input goto start
    ::echo %user_input%
    if /i %user_input%==y goto Yes
    if /i %user_input%==n (goto No) else (goto InvalidInput)
    
    :: /i for upercase, lowercase, or mixed case letters. It makes the comparison case-insensitive.
    
    :Yes
    echo user has entered yes
    pause
    goto start
    
    :No
    echo user has entered no
    pause
    goto start
    
    :invalidInput
    echo %user_input% is an Invalid input. Please enter 'y' or 'n'.` 
    set user_input=
    pause
    goto start
      



    How the Script Works


    #Step 1: Ask for User Input

    set /p user_input=Do you want to continue? (yes/no)?:


    The user's response is stored in the variable:
    user_input


    Example:

    y
    OR
    n




    #Step 2: Check for Empty Input

    if not defined user_input goto start

    If the user presses Enter without typing anything, the script returns to the beginning and asks again.



    #Step 3: Handle "Yes"

    if /i %user_input%==y goto Yes

    If the user enters:
    y


    The script jumps to:
    :Yes



    Output:

    user has entered yes




    #Step 4: Handle "No"

    if /i %user_input%==n (goto No)
    If the user enters:
    n


    The script jumps to:
    :No


    Output:
    user has entered no



    #Step 5: Handle Invalid Input

    If the user enters something else:

    abc


    The script jumps to:
    :InvalidInput


    Output:


    abc is an Invalid input. Please enter 'y' or 'n'.
    The variable is cleared and the user is asked again.




    What Does /I Mean?




    The `/i` option makes comparisons case-insensitive.



    Example:



    The following inputs will all be treated as valid:
    y
    Y

    Without `/i`, Batch would treat uppercase and lowercase letters differently.



    Sample Output



    #User Enters Yes

    Do you want to continue? (yes/no)? y
    user has entered yes


    #User Enters No

    Do you want to continue? (yes/no)? n
    user has entered no


    #User Enters Invalid Value
    Do you want to continue? (yes/no)? hello

    hello is an Invalid input. Please enter 'y' or 'n'.





    Real-World Uses



    System Engineers often use Yes/No prompts for:

    • * Confirming software installations
    • * Starting backups
    • * Restarting services
    • * Deleting files or folders
    • * Running maintenance tasks
    • * Executing administrative scripts


    This helps prevent accidental actions.




    Interview Questions


    #What does `set /p` do?
    It takes input from the user and stores it in a variable.


    #What does `if not defined` do?
    It checks whether a variable is empty.


    #What does `/i` mean in an IF statement?
    It makes the comparison case-insensitive.


    #Why is input validation important?
    It prevents users from entering invalid values and improves script reliability.


    #What is the purpose of `goto`?
    It transfers execution to a specified label.



    Conclusion


    A Yes/No prompt is a simple but useful feature in Batch Scripting. It allows scripts to interact with users, validate input, and perform different actions based on the response. By combining `set /p`, `if`, and `goto`, you can build more interactive and user-friendly automation scripts.



  • 0 comments:

    Post a Comment

    For Any Tech Updates, Hacking News, Internet, Computer, Technology and related to IT Field Articles Follow Our Blog.