Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 that command off my command buffer and mass overwriting uncommited changes.

What code would I need for my .bat file to make it say "are you sure", and make me type "y" before it ran the rest of the file? If anything but "y" is typed it should exit execution on that line.

When I call "exit;" it closes cmd.exe which is not what I want.

like image 692
Josh Ribakoff Avatar asked Nov 25 '09 04:11

Josh Ribakoff


People also ask

How do you answer yn in cmd?

Pipe the echo [y|n] to the commands in Windows PowerShell or CMD that ask “Yes/No” questions, to answer them automatically.

Are you sure y/n cmd?

The del command displays the following prompt: Are you sure (Y/N)? To delete all of the files in the current directory, press Y and then press ENTER. To cancel the deletion, press N and then press ENTER.

How do you prompt a batch file?

To have a batch file stop and prompt for input, you would want to look into using set /p , which will prompt for input and then assign that input to a variable. e.g.: set /P name="What Is Your Name? " echo Hello %name%!

What does %1 mean in a batch file?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.


1 Answers

You want something like:

@echo off setlocal :PROMPT SET /P AREYOUSURE=Are you sure (Y/[N])? IF /I "%AREYOUSURE%" NEQ "Y" GOTO END  echo ... rest of file ...   :END endlocal 
like image 81
Joe Avatar answered Sep 29 '22 10:09

Joe