Home > Blockchain >  How to make a custom "Press any key to continue" message in batch
How to make a custom "Press any key to continue" message in batch

Time:01-08

For an example "Press any key to return to command prompt"

what do I even put here lol.

CodePudding user response:

You could use a combination:

script00.bat:

@echo off

setlocal enableextensions

echo Press any key to return to command prompt
pause > nul

Output:

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q075040925]> script00.bat
Press any key to return to command prompt

CodePudding user response:

There can be used in the batch file the command line:

set /P "=Press any key to return to command prompt... " <nul & pause >nul & echo/

The caret is blinking in this case on same line as the prompt and not on the next line as it is the case on using:

echo Press any key to return to command prompt...
pause >nul

But in most cases can be simply used just pause to halt the batch file execution until the user pressed a key which is often not necessary on user executed the batch file from within a command prompt window.

There can be used the following command line to halt the batch file execution only if cmd.exe was started with option /c as first argument as done when double clicking on a batch file.

setlocal EnableExtensions EnableDelayedExpansion & for /F "tokens=1,2" %%G in ("!CMDCMDLINE!") do endlocal & if /I "%%~nG" == "cmd" if /I "%%~H" == "/c" pause

This command line does not run pause if the batch file is executed from within an opened command prompt window by a user who typed the batch file name and pressed RETURN or ENTER to execute it. It is often annoying for a user of a batch file starting it from within a command prompt window to press a key to end its execution.

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • cmd /?
  • echo /?
  • endlocal /?
  • for /?
  • help ... outputs an incomplete list of Windows commands with a brief description
  • pause /?
  • set /?
  • setlocal /?

See also:

  • Related