Batch file - stops in the middle of running - batch-file

I am trying to learn Batch.
I wrote a simple code with two labels.
This is the code:
#echo off
title intro
color 1f
::############################
::label
:one
cls
echo hello
echo. please enter your First name
echo.
:: fname is a variable (type p - string) that will content the user input. the input will be insert after <<
set /p fname= ">>"
echo.
echo. please enter your Last name
set /p lname=">>"
echo.
echo hello %fname% %lname%!
pause>nul
goto two
::############################
:two
cls
echo. welcome to page two
After the command line prints "hello ", it waits for the user response. if the user hits enter, instead of continuing to label "two", the command line closes.
Why?
Thanks.

Thats because it exits when the script comes to and end and has nothing more to do.
Add for example a pause at the very end of your script
Like this
echo. welcome to page two
pause
Then all you want on "page two" will be between your welcome to page two and pause

You don't say whether you are executing that batch from the prompt or from a shortcut"
After showing hello names entered the batch stops at the pause, and waits for an Enter. The >nul suppresses the prompt Press any key to continue . . .
batch will then continue to :two (the goto is redundant) but if you executed the routine from a "shortcut" the batch window will appear to close immediately as reaching end-of-file terminates the routine.

Related

How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?

If I have the following example code:
#echo off
:menu
cls
echo 1. win
echo 2. lose
set /p menu=
goto %menu%
pause>nul
:win
cls
echo yay lolz
pause>nul
:lose
cls
echo really?
pause>nul
How do I stop the batch from quitting if I type "test" instead of a valid response?
1. Documentations for the Windows commands
I suggest bookmarking in your browser:
Microsoft's command-line reference
An A-Z Index of the Windows CMD command line (SS64)
There can be get help for each Windows command by running in a command prompt window the command with /? as parameter, for example if /?, set /?, ... The execution of help results in an output of an incomplete list of Windows commands with a brief description.
2. Usage of SET /P for user prompts
It is advisable not using set /p if the user should choose from one of several offered options. There are multiple facts which must be taken into account on prompting a user for entering a string and assigning it to an environment variable:
The environment variable MyVar is not modified on usage of set /P "MyVar=Your choice: " if the user presses intentionally or by mistake just RETURN or ENTER. This means if the environment variable MyVar is not defined already before the user prompt, it is still not defined after the user prompt finished with just hitting key RETURN. And if MyVar is defined already before the user prompt, it keeps its value unmodified in case of the user presses just RETURN or ENTER. The command SET exits with error value 1 on user did not enter a string at all as documented by What are the ERRORLEVEL values set by internal cmd.exe commands?
The user has the freedom to type any string on being prompted with set /P. The batch file author has no control on what the user really enters. So the batch file author must take into account that the user enters by mistake or intentionally a string which could result in an exit of batch file execution because of a syntax error, or it does something completely different as it is defined for.
A simple example:
#echo on
:MainMenu
#set /P "menu=Your choice: "
if %menu% == 1 goto Label1
if %menu% == 2 goto Label2
goto MainMenu
:Label1
#echo Option 1 was chosen, fine.
exit /B
:Label2
#echo Option 2 was chosen, okay.
This batch file with echo on instead of echo off at top is started from within a command prompt window for debugging purposes.
Just RETURN is pressed on user prompt on first run. Windows command interpreter exits the batch file processing because first IF condition is preprocessed before execution of IF command to:
if == 1 goto Label1
There is obviously missing the first argument. cmd.exe encounters this syntax error and exits batch processing with an appropriate error message. The reason is the missing definition of environment variable menu which is not defined before user prompt and is still not defined after user prompt.
The string 2 is entered on second run of the batch file from within command prompt window and the batch file works as expected.
On third run of the batch file from within same command prompt window again just RETURN is pressed on user prompt. The batch file outputs again the second message. Why? The environment variable menu is still defined from second batch file execution with that string and the variable was not modified on pressing RETURN.
Okay, let us modify the example batch file to:
#echo on
:MainMenu
#set "menu=2"
#set /P "menu=Your choice: "
if "%menu%" == "1" goto Label1
if "%menu%" == "2" goto Label2
goto MainMenu
:Label1
#echo Option 1 was chosen, fine.
exit /B
:Label2
#echo Option 2 was chosen, okay.
This is already better as now environment variable menu is always predefined with value 2. So if the user enters nothing, a jump to Label2 is done. Also the value of previous run of variable menu has no effect anymore on execution of the batch file.
Another solution would be making use of exit code 1 on user not entering a string at all and define only in this case the environment variable with a default value by using:
#set /P "menu=Your choice: " || set "menu=2"
Single line with multiple commands using Windows batch file describes the conditional execution operator || to run the second command set "menu=2" only if the first executed prompt command set /P "menu=Your choice: " exits with an exit code not equal 0 as done when the user does not enter anything at all.
Thanks aschipfl for this contribution.
But is that really secure and fail safe now?
No, it isn't. The user still can enter by mistake a wrong string.
For example the user enters by mistake " instead of 2 which is easy on German keyboards as CapsLock+2 or Shift+2 results in entering ". The first IF command line after preprocessing is now:
if """ == "1" goto Label1
And this is again an invalid command line resulting in an exit of batch file processing because of a syntax error.
Let us assume a user enters on prompt the string:
" == "" call dir "%USERPROFILE%\Desktop" & rem
Note: There is a space at end.
The first IF condition is preprocessed by Windows command interpreter to:
if "" == "" call dir "%USERPROFILE%\Desktop" & rem " == "1" goto Label1
It can be seen that the batch file executes now a command not written in the batch file at all on both IF conditions.
How to get a user prompt fail safe and secure?
A user prompt can be made fail safe and secure by using delayed variable expansion at least for the code evaluating the string input by the user.
#echo on
:MainMenu
#setlocal EnableDelayedExpansion
#set "Label=MainMenu"
#set /P "menu=Your choice: " || set "menu=2"
if "!menu!" == "1" set "Label=Label1"
if "!menu!" == "2" set "Label=Label2"
endlocal & goto %Label%
:Label1
#echo Option 1 was chosen, fine.
exit /B
:Label2
#echo Option 2 was chosen, okay.
Now the user input string does not modify anymore the command lines executed by Windows command processor. So an exit of batch file processing because of a syntax error caused by user input is not possible anymore (fail safe). Furthermore, the batch file never executes commands not written in batch file (secure).
3. Usage of CHOICE for a choice prompt
There is a better command than set /P for a simple choice menu – CHOICE.
#echo off
:MainMenu
cls
echo/
echo 1 ... Option 1
echo 2 ... Option 2
echo E ... Exit
echo/
%SystemRoot%\System32\choice.exe /C 12E /N /M "Your choice: "
if errorlevel 3 exit /B
if errorlevel 2 goto Label2
if not errorlevel 1 goto MainMenu
#echo Option 1 was chosen, fine.
exit /B
:Label2
#echo Option 2 was chosen, okay.
The user has no freedom anymore to enter something not defined by batch file author. The batch file continues immediately after the user has pressed either 1, 2, E or Shift+E. Everything else is ignored by choice with exception of Ctrl+C.
The dynamic variable ERRORLEVEL has with three options nearly always a value in range 1 to 3 after choice terminated with returning 1 to 3 as exit code to calling cmd.exe. The exception is the rare use case that the user of a batch file pressed Ctrl+C on prompt and answers the next prompt Terminate batch job (Y/N)? of cmd.exe with N. In this case the dynamic variable ERRORLEVEL has the value 0 which is the reason for if not errorlevel 1 goto MainMenu to handle also this very special use case.
Note: if errorlevel X means IF GREATER OR EQUAL X. So it is always necessary to start with highest possible exit code of command choice.
As the exit code assigned to ERRORLEVEL is well known, it is possible on larger menus to optimize the code further by using appropriate labels:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "ERRORLEVEL="
:MainMenu
cls
echo/
echo 1 ... Option 1
echo 2 ... Option 2
echo E ... Exit
echo/
%SystemRoot%\System32\choice.exe /C 12E /N /M "Your choice: "
goto Label%ERRORLEVEL%
:Label0
rem The user pressed Ctrl+C and on next prompt N and
rem so made no choice. Prompt the user once again.
goto MainMenu
:Label1
#echo Option 1 was chosen, fine.
exit /B
:Label2
#echo Option 2 was chosen, okay.
exit /B
:Label3
The usage of command CHOICE can make choice menus very simple to code.
The third command line makes sure that there is not defined by chance an environment variable with name ERRORLEVEL which would otherwise prevent accessing the current value of dynamic variable ERRORLEVEL using with the exit code of command CHOICE using the syntax %ERRORLEVEL%.
Note: The usage of goto Label%ERRORLEVEL% is only possible with the choice menu command lines not being inside a command block starting with ( and ending with a matching ).
See also: How can I make an "are you sure" prompt in a Windows batch file?
Hint 1: There is a beep sound output by CHOICE if the user presses a not acceptable key. It is not possible to suppress that beep as there is no option offered by CHOICE to avoid output of the beep sound.
Hint 2: See also my answer on Where does GOTO :EOF return to? explaining also exit /B.

bat file programmatically press enter to go next line

When I run following command on cmd it ask me to press enter to continue. when I press enter it show the next thing and so on.we do this when develop/create a new firefox addon using jpm tool.first it show default title if I hit enter it show default name and etc...
command jpm init
here is an snapshot
now I want to make a bat file for this. so it should go to next line like when I press enter .
I tried this. but it doesn't go to next line it show title..and wait..
create.bat
call jpm init
echo
echo
echo
echo
echo
echo
pause
How can I make a bat file for this?
Try this:
(
echo/
echo/
echo/
echo/
echo/
echo/
) | call jpm init
pause
another method, that may (or may not) work for you:
<nul call jpm init
Whenever an input is required, it is taken from the NUL device.
Pro: no need to know how many inputs are required
Con: every input request is answered with RETURN

set /p empty answer crash

i am new here so i'll try to be as good as i can.
So i am trying to make a RPG based on text-based MS-DOS, and i am going pretty well as i just saw that if the user puts an invalid input at set /p, like an empty answer (just pressing enter) or an answer which is not on the "IF", the batch just crashes, and I would like to fix that so it will be less crashy.
Here is one of the parts i'd like to fix:
#echo off
title "Wasteland Adventure"
color 0A
cls
:Menu
cls
echo.
echo.
echo Welcome to Wasteland Adventure
echo.
echo To start a new game, type NEW and press ENTER.
echo To see instructions for the game, type INSTRUCTIONS and press ENTER.
echo To quit, type QUIT and press ENTER.
set input=
set /p input=What do you want to do?
if %input%==new goto INTRO
if %input%==instructions goto INSTRUCTIONS
if %input%==quit goto EXIT
Thanks in advance
it's not the set /pthat crashes, but:
if %input%==new
if %input% is empty, this is parsed as:
if ==new
obviously a syntax error. To avoid this, use:
if "%input%"=="new"
An empty input will then be parsed as:
if ""=="new"
which works fine.
The same applies when the variable contains only spaces and/or tabs:
if == new (syntax error) versus if " " == "new" (running fine)
Complete code like this:
:Menu
set input=
set /p input=What do you want to do?
if "%input%"=="new" goto INTRO
if "%input%"=="instructions" goto INSTRUCTIONS
if "%input%"=="quit" goto EXIT
REM for any other (invalid) input:
goto :Menu

Cursor in middle of text in batch

I am looking for a way to place the cursor between several lines of echo commands so it appears that the pause is in the center of the code, rather than at the end, while still displaying the last line of text and not continuing to the next label until hitting anykey. is this possible?
I want to have the appearance that the last line of actual text is a footer, seperat from the above text.
At the moment, my sequencing looks similar to this:
:LABEL
CLS
ECHO text1
ECHO.
ECHO.
(want the PAUSE to appear here)
ECHO.
ECHO.
ECHO text2
PAUSE (while the PAUSE is really here to prevent text2 from being lost)
GOTO OTHERLABEL
Thanks a bunch!
I think I know what you are asking, correct me if I am wrong. You want this functionality:
echo hello
pause
echo bye
But you want to switch the location on the screen of the pause and echo bye. If so, that is not easy. I suppose you could use a vbscript that once the first pause runs in the above code, would send a key to the batch file, which would echo. a ton of times and then pause again, and then the vbscript could scroll up in the batch file. It's not worth it.

Batch Script Game was not expected at this time

I'm a new batch programmer and am creating a batch "start menu". I'm using the following text and it's saying "X" was not expected at this time. X was 1, game, text, and other things. I can't figure it out. HELP! I'm running XP SP3 and using the command prompt. Also, if anyone spots any other mistakes please inform me. e-mail me at superzemus#hotmail.com
cls
#echo off
echo Welcome to the Start Menu!
echo.
echo Press ctrl-z to exit. Press G to play Adventure. Press T to enter Text Editor.
pause
set Game= %gme%
set Text= %txt%
IF select Game goto gme
IF select Text goto txt
:txt
echo You have chosen to enter the Text editor.
pause
start edit
:gme
echo You have chosen to play Adventure.
pause
start C:\Adventure.exe
What you want here is probably either set /p:
set /p choice=Press ctrl-z to exit. Press G to play Adventure. Press T to enter Text Editor.
and then compare as follows:
if /i %choice%==G goto Game
if /i %choice%==T goto Text
goto :eof
Or use choice which doesn't require you to press Return:
choice /m "Press Q to exit. Press G to play Adventure. Press T to enter Text Editor." /c GTQ /n
if errorlevel 3 goto :eof
if errorlevel 2 goto Text
if errorlevel 1 goto Game
goto :eof
There is also another problem with your code: You should exit the batch file after starting the editor to avoid starting the game too:
:Text
start edit
goto :eof
:Game
rem That's a horrible place to put something
start C:\Adventure.exe
goto :eof

Resources