Changing Pause Message - batch-file

Ok, so you know how when you type up pause in CMD, it will say 'Press any key to continue...'. How do I change that to say something like 'Press a key to proceed...'?
Lastly, I was coding a batch file. I want to know what's up if I have something like:
#echo off
cls
pause
pause
pause
pause
It seems to skip round about to pauses When you press a key. I'm curious as to know the rules of which the pauses are skipped.
Thanks.

Nearly the same as Deniz Zoeteman, except this version displays the blinking cursor on the same line as your custom message, as does the normal PAUSE command. The Deniz Zoeteman solution displays the blinking cursor below your message.
<nul set /p "=Press a key to proceed..."
pause >nul

You cannot change the text displayed when a pause command is executed. It's bound to the Windows installation's language pack. The only thing you can do is not letting it say anything by doing pause>nul.
Of course, there's different ways to simulate pause; see the example from the other answer, where set /p is used. With pause>nul however, you can do this:
echo Custom pause message
pause>nul
And that should work.
And for pause commands skipping, that's most likely due to the key still being pressed down while the next pause command already executed (small guess though - I don't recall exactly if that's the behaviour of the command).

You may want to try
set /p=your message
Note: you will have to hit the enter key to continue versus any key.

Option #1:
#echo off
echo Press any key to continue or Ctrl-C to abort.
pause > nul
Option #2
set /p=Press any key to continue or Ctrl-C to abort.
-->> I prefer the screen output of option #1.

This is one practical application:
set /p=Press ENTER key to proceed or Ctrl-C to abort.

Related

Specific Keystroke for batch

I am trying to find out how to make batch wait for a specific keystroke. I don't want any of those pause>nul echo Press Enter to continue... None of that stuff, I would like it so that way once you press a key (eg. a) then it will go to :Test, if you press another key then it won't react what so ever. And I looked at the first page result, and went to the links on Google.
I believe the solution to your problem is the CHOICE command. This will allow you to set a key, have the batch script wait for that key to be pressed, then continue. It also avoids any cryptic pausing or loops in the script.
Keep in mind that the choice command has a few syntax's
/C - Specifies the list of choices to be created.
/N - Hides the list of choices in the prompt.
/CS- Enables case-sensitive choices to be selected.
/T - The number of seconds to pause before a default choice is made.
/D - Specifies the default choice after timeout seconds.
/M - Specifies the message to be displayed before the prompt.
For the sake of your usage, the following script can be used. Its also important to note you can have multiple keys act as your specific keystroke by doing ex: 12345 for SET KEY=
#ECHO OFF
::Change this key to what ever you wish.
SET KEY=N
:Start
cls
echo Press %KEY% to continue...
choice /c %KEY% /n /cs
if %errorlevel%==1 goto Continue
:Continue
cls
echo Hello World!
pause.

Catching keystroke during loop in batch

As a small training for my batch programming I originally followed the idea to make up a small slotmachine. You can give your coins, the numbers rush through and when you hit a button, the numbers should stop at their current value. If they are all the same, boom, you won the jackpot!
My problem right now is the keystroke capture during the loop. I already thought something like a choice command, but then the program would stop at each loop waiting for keyboard input, not making the game just quite annoying to wait all the time, but as well boring as you could check if you want to hit a specific button to stop.
Another thought was putting
set /p foobar=
and then simulate an Enter-Stroke with !SendKeys! (with everything neccessary in the code), forgetting that the enter yould have been sended after input...
Is there a way to accomplish that in ONE batch file? Or do I have to come up with another one to simulate the keystrokes or is there anything else I have missed?
EDIT: To clearify: Is there any command that changes something on keystroke, but runs through if nothing is touched?
Thanks for help in advance!
Greetings
geisterfurz007
#echo off
setlocal EnableDelayedExpansion
rem Create an empty file
cd . > key.txt
rem Start a parallel process that wait for Enter key
rem and add a line to the empty file
start "" /B cmd /C "set /P = & echo line >> key.txt"
set "key="
:wait
cls
set "number=%random:~-4%"
echo %number%
echo/
echo Press Enter key to stop the numbers...
set /P key=< key.txt
if not defined key goto wait
echo The last number is: %number%

goto different labels depending of value of variable that may be empty or undefined

I worked with 4DOS a lot decades ago, and bash more recently, but don't have experience with plain Windows batch. I'm trying to make something to conveniently kill the firefox.exe processes that sometimes misfire and never show Firefox but persist and eat my resources.
Trying to make a query-to-user that defaults to kill the processes.
The first problem is the "if %REPLy%==SOMETHING (goto SOMEWHERE)" statements.
The explicit ones work fine but I want to kill the firefoxes if none of them are true. I thought I'd just put the code after the "if...goto"s but that didn't work. So I tried an additional if based on the variable REPLy equaling nothing. That didn't work. So I thought maybe a variable equaling nothing ("") might not be the same as being undeclared and maybe the reply stuff was simply removing the variable rather than giving the value "" and added an if for that. That didn't work either. So I thought maybe I had to put the kill code under a label and send execution there with a goto like the I did in the statements that work, but that doesn't work either. If I enter SOMETHING other than the explicit variations of NO or no, full or truncated, it works, kinda. The taskkill command reports success but it still fails in reality. But I'll work on that bridge when I get there. The immediate problem is how to get NO entry (in other words, just hit the enter key) to goto the kill code just like a non-no string does. What am I doing wrong here?
#echo off
REM All this stuff with the path is because I can't reboot this system right now (long story) and I can not seem to make the amended path stick. So for now, I set it each time. I presume I just need to reboot to make the path setting I changed under computer properties, etc, stick.
echo "This is the path:"
path
PATH=%PATH%;C:\Program Files\GnuWin32\bin
echo "This is the path now:"
path
echo "All these path setting and testing commands and remarks can be cleaned up after I figure out if the new path becomes permanent after reboot."
REM Here ends the stuff I expect to delete after I can reboot.
REM Here begins the part I do not expect to change and that works fine.
tasklist | findstr /B firefox.exe | wc -l > kill_firefox.bat_var.tmp
set /p NUMBER_OF_PROCESSEs=<kill_firefox.bat_var.tmp
del kill_firefox.bat_var.tmp
IF NOT DEFINED NUMBER_OF_PROCESSEs (goto ERROR - NUMBER_OF_PROCESSEs not set)
if %NUMBER_OF_PROCESSEs%==0 (goto NO_PROCESSES)
REM Since the contrary conditions lead to gotos, if processing gets to here, there are 1 or more firefox.exe processes.
echo The number of firefox.exe processes running is:
echo .
echo %NUMBER_OF_PROCESSEs%
echo .
tasklist | findstr /B firefox.exe
set /p REPLy= "Kill these? Y/n"
echo "REPLy is %REPLy%"
pause
if %REPLy%==n (goto USER DECLINED TO KILL)
if %REPLy%==N (goto USER DECLINED TO KILL)
if %REPLy%==no (goto USER DECLINED TO KILL)
if %REPLy%==NO (goto USER DECLINED TO KILL)
REM Here is where the problems start. By my reasoning, I shouldn't need any if/then here, nor even a goto, just the code that is now in the part labeled KILL. The ifs and the goto and putting the code in the labeled section are the result of many attempts to get that code to run with various constructions.
if [%REPLy%] == [] goto KILL
IF NOT DEFINED REPLy (goto KILL)
goto KILL
:NO_PROCESSES
echo There are no firefox.exe processes running.
pause
exit
:ERROR - NUMBER_OF_PROCESSEs not set
echo Logic error - The variable is not defined. This script must be repaired.
pause
exit
:USER DECLINED TO KILL
echo User declined to kill processes.
pause
exit
:KILL
REM I am not sure if ANY of this is running because the pause command is not working and the terminal disappears to fast to see. What am I doing wrong here?
echo killing . . .
taskkill /IM firefox.exe
pause
exit
Added by edit:
OK, I musta confused my smart pills with my dumb pills. Here is how I fixed the part that I was stuck on:
I changed
set /p REPLy= "Kill these? Y/n"
to
set /p REPLy= "Kill these? Y/n" || set REPLy=Y
and that did the trick. I see why that works, but I don't quite see why the ways I tried before don't. Apparently Batch treats variables set (unset? cleared? nulled?) by "set /p somemessage" with just a plain enter key as a response in some way I don't understand. But anyway, I don't have to understand it, just accept it. The construction with the "||" above works. Anyway this was the part of the problem I asked about and it's solved. If I can't get the rest of it working I'll post again after cleaning this batch file up a bit.
use setx to set a permanent variable (see setx/?, the syntax is different from set).
set /p leaves the variable unchanged, if input is empty. So you can predefine a variable:
set "REPLy=Y"
set /p "REPLy=Kill these? Y/n"
echo %REPLy%
echo first letter of REPLy is %REPLy:~0,1%
but instead of set /p, I would use choice.
There is no "empty" variable. If it has no value, the variable is not defined.
if has a /i switch to ignore capitalization.
To get the number of processes I would use (no need for an external utility):
for /f %%i in ('tasklist ^| find /c "firefox.exe"') do set NUMBER_OF_PROCESSEs=%%i
and as SomethingDark already mentioned:
run the script from the command prompt instead of double clicking it (and use exit /b instead of exit)
don't use spaces in labels.

How do I test for button press on pause command in batch?

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 that pause doesn't take input, I'm just using it as a placeholder for something
pause>nul
cls
echo Welcome!
pause
Is there any way to accomplish this? Thanks!
To clarify, I want to exit the second they press the E key. I have seen it done before, but I forgot where it saw it.
Choice.exe does what you describe, though it doesn't have an option for "any key". You can specify a specific key such as "Y" below.
echo once you fully understand this message, press "Y" to continue, or press "E" to exit.
choice /c ey
if errorlevel 2 goto :welcome
if errorlevel 1 goto :canceled
rem User pressed Ctrl+C
:canceled
rem User pressed E. Do cancel stuff.
goto :eof
:welcome
rem User pressed Y. Do welcome stuff
echo Welcome!

MS-DOS Batch file pause with enter key

Is it possible in MS-DOS batch file to pause the script and wait for user to hit enter key?
I wish to do this inside a for loop. After each iteration, I want the script to pause and wait for user to hit 'Enter'
There's a pause command that does just that, though it's not specifically the enter key.
If you really want to wait for only the enter key, you can use the set command to ask for user input with a dummy variable, something like:
set /p DUMMY=Hit ENTER to continue...
You can do it with the pause command, example:
dir
pause
echo Now about to end...
pause
pause command is what you looking for.
If you looking ONLY the case when enter is hit you can abuse the runas command:
runas /user:# "" >nul 2>&1
the screen will be frozen until enter is hit.What I like more than set/p= is that if you press other buttons than enter they will be not displayed.
Depending on which OS you're using, if you are flexible, then CHOICE can be used to wait on almost any key EXCEPT enter
If you are really referring to what Microsoft insists on calling "Command Prompt" which is simply an MS-DOS emulator, then perhaps TIMEOUT may suit your purpose (timeout /t -1 waits on any key, not just ENTER) and of course CHOICE is available again in recent WIN editions.
And a warning on SET /P - whereas set /p DUMMY=Hit ENTER to continue... will work,
set "dummy="
set /p DUMMY=Hit ENTER to continue...
if defined dummy (echo not just ENTER was pressed) else (echo just ENTER was pressed)
will detect whether just ENTER or something else, ending in ENTER was keyed in.
The only valid answer would be the pause command.
Though this does not wait specifically for the 'ENTER' key, it waits for any key that is pressed.
And just in case you want it convenient for the user, pause is the best option.
npocmaka's answer made me aware of runas /user:# "" >nul 2>&1 – however, I've discovered that it can be shortened significantly, thanks to an undocumented parameter alias. 2>&1 also appears to be unnecessary. This is as short as you can make it (not counting the prompt text):
echo|set/p="Press <ENTER> to continue.."&runas/u: "">NUL
It's not very readable with syntax highlighting (and stackoverflow.com still hasn't fixed their batch file syntax highlighting), so here's a screenshot with syntax highlighting:

Resources