How can I make an “are you sure” prompt in a Windows batchfile?

前端 未结 11 1212
时光说笑
时光说笑 2020-12-07 19:43

I have a batch file that automates copying a bunch of files from one place to the other and back for me. Only thing is as much as it helps me I keep accidentally selecting t

相关标签:
11条回答
  • 2020-12-07 20:22

    The choice command is not available everywhere. With newer Windows versions, the set command has the /p option you can get user input

    SET /P variable=[promptString]
    

    see set /? for more info

    0 讨论(0)
  • 2020-12-07 20:25

    If you want to the batch program to exit back to the prompt and not close the prompt (A.K.A cmd.exe) you can use "exit /b".

    This may help.

    set /p _sure="Are you sure?"
    ::The underscore is used to ensure that "sure" is not an enviroment
    ::varible
    if /I NOT "_sure"=="y" (
    ::the /I makes it so you can
    exit /b
    ) else (
    ::Any other modifications...
    )
    

    Or if you don't want to use as many lines...

    Set /p _sure="Are you sure?"
    if /I NOT "_sure"=="y" exit /b
    ::Any other modifications and commands.
    

    Hope this helps...

    0 讨论(0)
  • 2020-12-07 20:25

    Open terminal. Type the following

    echo>sure.sh
    chmod 700 sure.sh
    

    Paste this inside sure.sh

    #!\bin\bash
    
    echo -n 'Are you sure? [Y/n] '
    read yn
    
    if [ "$yn" = "n" ]; then
        exit 1
    fi
    
    exit 0
    

    Close sure.sh and type this in terminal.

    alias sure='~/sure&&'
    

    Now, if you type sure before typing the command it will give you an are you sure prompt before continuing the command.

    Hope this is helpful!

    0 讨论(0)
  • 2020-12-07 20:30

    Here a bit easier:

    @echo off
    set /p var=Are You Sure?[Y/N]: 
    if %var%== Y goto ...
    if not %var%== Y exit
    

    or

    @echo off
    echo Are You Sure?[Y/N]
    choice /c YN
    if %errorlevel%==1 goto yes
    if %errorlevel%==2 goto no
    :yes
    echo yes
    goto :EOF
    :no
    echo no
    
    0 讨论(0)
  • 2020-12-07 20:31

    First, open the terminal.

    Then, type

    cd ~
    touch .sure
    chmod 700 .sure
    

    Next, open .sure and paste this inside.

    #!/bin/bash --init-file
    PS1='> '
    alias y='
        $1
        exit
    '
    alias n='Taskkill /IM %Terminal% /f'
    echo ''
    echo 'Are you sure? Answer y or n.'
    echo ''
    

    After that, close the file.

    ~/.sure ; ENTER COMMAND HERE
    

    This will give you a prompt of are you sure before continuing the command.

    0 讨论(0)
提交回复
热议问题