Home > Articles > Home & Office Computing > Microsoft Windows Desktop

  • Print
  • + Share This
This chapter is from the book

Prompting for Input

If your batch file has to print a message you definitely don't want the users to miss, use the pause statement to make the batch file sit and wait until they've read the message and acknowledged it. Here's an example:

echo The blatfizz command failed. This means that the world as 
echo we know it is about to end, or, that the input file needs to
echo be corrected.
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. Here's an example:

:again 
   echo The input file INPUT.DAT does not exist
   set /p answer=Do you want to create it now (Y/N)?
   if /i "%answer:~,1%" EQU "Y" goto editit
   if /i "%answer:~,1%" EQU "N" exit /b
   echo Please type Y for Yes or N for No
   goto again

These commands ask the user to type a response, examine the leftmost letter with %answer:,1%, and take the appropriate action only if the user types a valid response. In fact, this is a good pattern to remember.

  • + Share This
  • 🔖 Save To Your Account