Batch file: press any key except X to exit

batch file press any key to continue
surpass the 'press any key to continue' in batch script
batch file pause for keypress
batch file detect keypress
batch file loop until keypress
batch choice
vbscript pause until key pressed
exit batch script without closing window

I want to present a user of a batch file with an option to "press X to continue, or any other key to exit". I would like "any other key" to include the space bar, or any of the other keys that usually cause the batch file to terminate when you use pause>nul

I know how to structure the code to either exit or continue once I have a "user pressed X" or "user pressed something other than X" result. I just can't work out how to get that result.

I don't want to use the Set /P user input method because that requires them to press an alphanumeric key and then press enter.

I can't find a way to make the Choice /C method accept "any key" and only act on one particular one.

TL;DR does anyone have a way to a way to allow the user of a batch file to press a single key, and then have the file determine whether that key was X or Not X?

You might use xcopy together with its /W option to press a key before copying and the /L switch to actually not copy anything but list files that would be copied:

@echo off
set "KEY=" & for /F "delims=" %%K in ('
    2^> nul xcopy /L /W /I "%~f0" "%TEMP%"
') do if not defined KEY set "KEY=%%K"
setlocal EnableDelayedExpansion
if /I "!KEY:~-1!"=="X" goto :MoreCode
endlocal & exit /B

:MoreCode
echo {X} has been pressed.
pause

To check the pressed key delayed expansion is enabled in order to avoid syntax errors in case you pressed ".

how to do "press enter to exit" in batch, If rake is another batch file, command interpreter switches to it and exits Press any key to continue . I am writing a batch script, in which a user reads a disclaimer, then they press any key to continue, or "E" to exit. It looks something like this: @echo off echo (some disclaimer text here) echo. echo once you fully understand this message, press any key to continue, or press "E" to exit.

I know you said that you didn't want to use Set /P, but I'm not sure if the issue was with the alphanumeric key or with the additional enter key press.

The following, although untested, shouldn't require the additional enter key press:

@Echo Off
Set "#="
Set /P "=Press X to continue :"<Nul
For /F Skip^=1^ Delims^=^ EOL^= %%$ In ('Replace ? . /U /W') Do If Not Defined # Set "#=%%$"
Echo(
If /I Not "%#%"=="X" Exit /B
Rem Your code goes here

batch script - press any key to exit, all, i have a list steps that my batch script will do ie uninstall/install Unless I'm missing something. Cancel or the [X] close button to abort the restart"&VbCrLf& _ "Do nothing and the  I created a batch file however, I am want a command that will prompt the user to echo "Press any key to exit" and that once the key is entered it will exit. Comment Premium Content

You can use the choice command:

@echo off
choice /c abcdefghijklmnopqrstuvwxyz0123456789 /n /m "Press X to continue, or any other key to exit."
cls
if not "%errorlevel%" == "24" exit

And then you can continue on with your program.

OS X Yosemite All-in-One For Dummies, The command line gives you a number of ways to search for files on your hard drive. The most commonly used search  I have a DOS batch file that show some results on the cmd console. I want users to press Enter to run program or press any other key on the keyboard to exit. I don't want to use options/choices because in that case user will have to enter at least something which I need to check and set the action.

PC Mag, BAT), is similar except that it supplies the WordStar keystrokes to change the help When it exits back to the batch file, WSP.BAT does a SHIFT command, making %l the next file in the list, and goes through the routine again. Press any key. And every batch file needs to quit, and will do so, when it reaches the last line of code. There are several ways to end batch file execution, like reaching the end of the batch file, starting execution of another batch file, or using the EXIT or GOTO:EOF commands. Each operating system may react in its own way to any of these events.

Press space to continue, pause(){ read -n1 -rsp $'Press any key to continue or Ctrl+C to exit\n' }. Then you can use this everywhere in your  i have a list steps that my batch script will do ie uninstall/install programs and then shutdown the pc. my first step is that im trying to make a batch script, basically if they press any key within the time limit set it will quit the continuing steps. abit like the timeout command. timeout /t 60

Create a batch file that prompts user to press any key to exit , I created a batch file however, I am want a command that will prompt the user to echo "Press any key to  pause exit /b If you want to ask a user whether to proceed after a mishap, or if you want the batch file to prompt for input filenames or other data, you can use the new extended set /p command. Set /p reads a user's response into an environment variable, where it can be tested or used as a command argument.

Comments
  • Type choice /C ? and you'll see what choices/keys that command accepts (? is an invalid one, so choice returns a helpful error message)...
  • Neat! Outside the box solution that does exactly what I need. I made a small edit to your solution for the sake of completeness - the way you wrote it initially, pressing X would echo "X has been pressed" and then exit, while pressing any other key would immediately exit. With my edits, pressing x jumps to a "more code" section, echoes "X has been pressed" and then pauses, while pressing any other key exits immediately.
  • Great that you like it! I just changed exit to exit /B because the former terminates also the cmd instance the batch file runs in while the latter just exits the batch file...
  • While here, before Rem Your code goes here add echo. to add a newline because if X is pressed and something echoed again, it will be in the same line as Press X to continue :.
  • @double-beep, echo/ would be better. Anyway, I could imagine Compo wanted to echo the pressed key behind the message...?
  • I guess you don't need the if not defined # (if I remember correctly the output of replace); and I think pressing " causes a syntax error... But a nice one anyway!
  • It was a bit of both - the current working file has a "press any key to end", and now I'm adding a "press X to do one more thing, or any other key to end", so I'd rather not change the workflow by forcing an enter key to be pressed. I also know that whenever I use the file, I tend to press the space bar to end, having to press an alphanumeric key is less than ideal
  • And what if the user presses one of the following characters: ~!@#$%^&*()_+}{":><?|\-=[]';></? Please test your answers before posting them. You seem to have a bunch of pure answers -- one of them got accepted, also.