Batch file closing immediately once launched - batch-file

I have this batch script that is supposed to be the menu of a game. I haven't finished it yet but I figured out that when i run it it immediately closes. If anyone could please help me figure out the issue I would greatly appreciate it, thank you. Here is the code:
#echo off
set save_file_path=C:\Users\maxqs\Desktop\Coding Projects\Personal Project\PathRPG\save_file.txt
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo :oooo+/` -: dy :ooooo/. `ooooo/- -+syys+-
echo oMo::/yMy dM Nm sMo::/yMy `Md::/oNm. :mm+-.-:od+
echo oM/ hM- :dddddy- hNMhhh. Nmohdmd/ sM: mM` `Mh /My :Mh
echo oM/ `/Nd `` `dN dM NN: hM. sM/..-sMo `Mh -dN: hM- `````
echo oMmddhs/ -sdhhhNM` dM Nm oM: sMdydMd` `MNdddy+` yM: +hhmMo
echo oM/ .Md` yM` dM Nm oM: sM: /Nm: `Mh :Md` /Mo
echo oM/ `mm/:/sNM` sMo:/` Nm oM: sM: `sMy` `Mh :dNs/::/yM+
echo -/. `/oo+.:/ :+o+` // -/` -/. -// `/: ./ooo+:.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo [1] Start game
echo [2] End game
set /p input= >
if %input%== 1 goto end_menu
if %input%== 2 goto start_game
:start_game
echo [1] New game
echo [2] Continue
echo [3] Options
set /p input= >
if %input%== 1 goto new_game
if %input%== 2 goto continue_game
if %input%== 3 goto options
:new_game
echo 000#>%save_file_path%
CALL game.bat
goto end_menu
:end_menu

Here's what you code should look like, it uses doublequotes, and parentheses to protect intended strings, and choice.exe as the correct and appropriate command to use for requesting use input.
#echo off
set "save_file_path=C:\Users\maxqs\Desktop\Coding Projects\Personal Project\PathRPG\save_file.txt"
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo :oooo+/` -: dy :ooooo/. `ooooo/- -+syys+-
echo oMo::/yMy dM Nm sMo::/yMy `Md::/oNm. :mm+-.-:od+
echo oM/ hM- :dddddy- hNMhhh. Nmohdmd/ sM: mM` `Mh /My :Mh
echo oM/ `/Nd `` `dN dM NN: hM. sM/..-sMo `Mh -dN: hM- `````
echo oMmddhs/ -sdhhhNM` dM Nm oM: sMdydMd` `MNdddy+` yM: +hhmMo
echo oM/ .Md` yM` dM Nm oM: sM: /Nm: `Mh :Md` /Mo
echo oM/ `mm/:/sNM` sMo:/` Nm oM: sM: `sMy` `Mh :dNs/::/yM+
echo -/. `/oo+.:/ :+o+` // -/` -/. -// `/: ./ooo+:.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo [1] Start game
echo [2] End game
choice /c 12 /n /m ">"
if errorlevel 2 goto end_menu
:start_game
echo [1] New game
echo [2] Continue
echo [3] Options
choice /c 123 /n /m ">"
if errorlevel 3 goto options
if errorlevel 2 goto continue_game
:new_game
(echo 000#) > "%save_file_path%"
CALL "game.bat"
pause
goto end_menu
:end_menu
BTW, I removed the unnecessarily pointless and wasteful trailing space characters from your code, all 5376 of them obviously increased your script size by 5Kb too.

Related

Batch variables do not work

I have the following batch script:
#echo off
color 2f
title "USERS MANAGEMENT IN A LAN"
:top
echo ***************************************************************
echo.
echo SELECT ACTIONS
echo.
echo ***************************************************************
echo.
echo [1] Ping the service Params: unu
echo [2] TBD
echo.
echo [e] Exit
echo.
echo ***************************************************************
echo Enter the number of the website which you would like to go to:
echo.
set /p udefine=
echo.
echo ***************************************************************
if %udefine%==1 (
echo Give the parameters:
set /p argi=
set /p argii=
START /B java -jar JavaClient.jar %argi% %argii%
)
if %udefine%==e goto exit
cls
echo ***************************************************************
echo.
echo Thank You for using it
echo.
echo ***************************************************************
echo Type [e] to exit or [b] to go back and select another option.
echo.
set /p udefine=
echo.
echo ***************************************************************
if %udefine%==b goto top
if %udefine%==e goto exit
:exit
cls
echo ***************************************************************
echo.
echo Thank You for using it
echo.
echo ***************************************************************
pause
exit
The problem is that if I run it on the first time, the java jar file is throwing exceptions because it doesn't understands the variable arguments %argi% and %argii%. I press [b] to start again at the options menu, I do the same thing again and voila, it works! Why? Why it doesn't work at the first time, and if i press [b] to go back and put again the args it works fine every time?
I start the app:
If I kill the script with Ctrl-C and reopen it, it will have the same behaviour again...first time error, and then success.
Thank you!
you need delayed expansion :
#echo off
setlocal enableDelayedExpansion
color 2f
title "USERS MANAGEMENT IN A LAN"
:top
echo ***************************************************************
echo.
echo SELECT ACTIONS
echo.
echo ***************************************************************
echo.
echo [1] Ping the service Params: unu
echo [2] TBD
echo.
echo [e] Exit
echo.
echo ***************************************************************
echo Enter the number of the website which you would like to go to:
echo.
set /p udefine=
echo.
echo ***************************************************************
if %udefine%==1 (
echo Give the parameters:
set /p argi=
set /p argii=
START /B java -jar JavaClient.jar !argi! !argii!
)
if %udefine%==e goto exit
cls
echo ***************************************************************
echo.
echo Thank You for using it
echo.
echo ***************************************************************
echo Type [e] to exit or [b] to go back and select another option.
echo.
set /p udefine=
echo.
echo ***************************************************************
if %udefine%==b goto top
if %udefine%==e goto exit
:exit
cls
echo ***************************************************************
echo.
echo Thank You for using it
echo.
echo ***************************************************************
pause
exit

Progress bar shows "echo is OFF" message

I'm trying to simulate a progress bar with ASCII characters, but any time my progress bar is more than 0% full, the message echo is OFF is displayed.
#echo off
setlocal EnableDelayedExpansion
color A
echo hacking main intelligence systems...
ping -n 2 127.0.0.1>nul
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo Loading... Please Wait
echo ---------------------------------------
echo :0 ]
echo ---------------------------------------
ping localhost -n 2 >nul
cls
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo Loading. Please Wait
echo ---------------------------------------
echo || :5 ]
echo ---------------------------------------
ping localhost -n 3 >nul
cls
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo Loading.. Please Wait
echo ---------------------------------------
echo |||| :15 ]
echo ---------------------------------------
ping localhost -n 2 >nul
The script continues on like that for a while, but this snippet is smaller and has the same issue.
What is causing the message to appear, and how can I correctly display the bar progressing?
Your actual question of why your echos aren't working is perfectly reasonable, and it's an extremely common error for beginners.
| is a symbol that takes the output of one command and uses it as the input of another command. In your case, you effectively have echo by itself, which will simply return the status of echo (in this case, echo is OFF).
In order to get around this, you can either use a different symbol, or you can open your code in a text editor and replace all instances of | with ^|.
^ is an escape character, which tells echo to interpret the symbol literally and print an actual | instead of using it to route command output.
#echo off
setlocal EnableDelayedExpansion
color A
echo hacking main intelligence systems...
ping -n 2 127.0.0.1>nul
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo Loading... Please Wait
echo ---------------------------------------
echo :0 ]
echo ---------------------------------------
ping localhost -n 2 >nul
cls
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo Loading. Please Wait
echo ---------------------------------------
echo ^|^| :5 ]
echo ---------------------------------------
ping localhost -n 3 >nul
cls
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo Loading.. Please Wait
echo ---------------------------------------
echo ^|^|^|^| :15 ]
echo ---------------------------------------
ping localhost -n 2 >nul

Batch file closing instead using "goto" command

My code is not completely finished but it was working.Now it closes after the first section instead of going to section two.I know that this is probably a dumb mistake but I would appreciate the help!(The lines are just for organization)
#echo off
title First batch file
color 1f
::-----------------------
----------
:one
cls
echo Please enter your
name below.
echo.
echo.
set /p name=Name:
echo.
echo.
echo Hello %name%!
echo.
echo.
pause >nul
goto two
::-----------------------
----------
:two
cls
echo Would you like to
create a new account?
echo (yes or no)
echo.
echo.
set /p yorn=>>
if yorn equ yes then goto
new
if yorn equ no then goto
login
pause >nul
::-----------------------
---------
:new
cls
echo Please enter your
user name.
echo.
echo.
set /p username=Username:
cls
echo.
echo Please enter your
password.
echo.
echo.
set /p password=Password:
cls
echo.
echo Please confirm your
password.
echo.
echo.
set /p cpassword=Confirm
password:
cls
echo.
echo.
if %password% equ
%cpassword% goto
confirmscreen
if %password% new
%cpassword% goto new
pause >nul
:------------------------
---------
:confirmscreen
cls
echo Confirm:
echo.
echo.
echo Username: %username%
echo.
echo Password: %password%
echo.
echo.
set /p yorn2=Yes or No?:
if yorn2 equ yes goto
login
if yorn2 equ no goto new
pause >nul
:------------------------
---------
#echo off
title First batch file
color 1f
::---------------------------------
:one
cls
echo Please enter your name below.
echo.
echo.
set /p name=Name:
echo.
echo.
echo Hello %name%!
echo.
echo.
pause >nul
goto two
::---------------------------------
:two
set /p yorn=Would you like to create a new account? (yes or no) :
if %yorn% equ yes then goto new
if %yorn% equ no then goto login
goto:two

How to make this BATch file have a timer (time left to shutdown or reset)?

How to make this BATch file have a timer?
Would like to see how much time left. Windows only says "Windows will shut down in less then a minute".
#echo off
color 0A
title Restart\/Shutdown
:start
echo Welcome, %USERNAME%
echo What would you like to do?
echo.
echo 1. Shut down
echo 2. Restart
echo.
echo 0. Quit
echo.
set /p choice="Enter your choice: "
if "%choice%"=="1" goto shutdown
if "%choice%"=="2" goto restart
if "%choice%"=="0" exit
echo Invalid choice: %choice%
echo.
pause
cls
goto start
:shutdown
shutdown.exe -s -t 10
:restart
shutdown.exe -r -t 10
You could use the command "timeout /t 10" or something like:
#ECHO OFF
FOR /L %%# IN (10,-1,1) DO (SET/P "=%%# seconds left... "<NUL:
PING -n 2 127.0.0.1 >NUL
CLS:)
#ECHO off
cls
color 0B
:taskkill
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo Program To Shutdown
echo.
set /p taskkill=
if "%taskkill%" == "%taskkill%" goto taskkillconfirm
exit
:taskkillconfirm
color 0B
:image
set image=/im
if "%image%" == "%image%" goto imageconfirm
exit
:imageconfirm
color 0B
:forced
set forced=/f
if "%forced%" == "%forced%" goto forcedconfirm
exit
:forcedconfirm
:hour
color 0B
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo. Hours?:
echo.
set /p hour=
:min
color 0B
cls
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo Minutes?:
echo.
set /p min=
:sec
color 0B
cls
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo Seconds?:
echo.
set /p sec=
:continue
color 0B
cls
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo %taskkill% Shutdown in
echo.
echo %hour% Hours %min% Minutes %sec% Seconds
set /a sec="%sec%-1"
if %sec%==-1 set /a min="%min%-1"
if %sec%==-1 set /a sec="59"
if %min%==-1 set /a hour="%hour%-1"
if %min%==-1 set /a min="59"
if %hour%==-1 goto done
ping -n 2 127.0.0.1 >NUL
goto continue
:done
color 0B
cls
taskkill %image% %taskkill% %forced%
exit
" ping localhost -n 10 >nul " take off quotations and to edit the amount of seconds replace the 10 with whatever you

Batch animation doesn't work at runtime

when i run it does not pause after the fist frame please help
#echo off
title line!
color 0a
echo.
echo -----------------------------------
echo press a key to Make The line spin
echo -----------------------------------
echo.
echo Made By: alex.page
echo.
pause >nul
echo -----------------
echo.
echo.
echo.
echo.
echo ---
echo.
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
echo ------------------
echo.
echo.
echo.
echo \
echo \
echo \
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
echo ------------------
echo.
echo.
echo.
echo |
echo |
echo |
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
echo ------------------
echo.
echo.
echo.
echo /
echo /
echo /
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
echo ------------------
echo.
echo.
echo.
echo.
echo ---
echo.
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
echo ------------------
echo.
echo.
echo.
echo \
echo \
echo \
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
echo ------------------
echo.
echo.
echo.
echo |
echo |
echo |
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
echo ------------------
echo.
echo.
echo.
echo /
echo /
echo /
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
echo ------------------
echo.
echo.
echo.
echo.
echo ---
echo.
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
echo ------------------
echo.
echo.
echo.
echo.
echo ---
echo.
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
echo ------------------
echo.
echo.
echo.
echo \
echo \
echo \
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
echo ------------------
echo.
echo.
echo.
echo |
echo |
echo |
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
echo -----------------
echo.
echo.
echo.
echo /
echo /
echo /
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
echo -----------------
echo.
echo.
echo.
echo.
echo ---
echo.
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
echo -----------------
echo.
echo.
echo.
echo \
echo \
echo \
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
echo -----------------
echo.
echo.
echo.
echo |
echo |
echo |
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
echo ------------------
echo.
echo.
echo.
echo /
echo /
echo /
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
echo -----------------
echo.
echo.
echo.
echo.
echo ---
echo.
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
echo ------------------
echo.
echo.
echo.
echo.
echo ---
echo.
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
echo ------------------
echo.
echo.
echo.
echo \
echo \
echo \
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
echo -----------------
echo.
echo.
echo.
echo |
echo |
echo |
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
echo -----------------
echo.
echo.
echo.
echo /
echo /
echo /
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
echo -----------------
echo.
echo.
echo.
echo.
echo ---
echo.
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
echo -----------------
echo.
echo.
echo.
echo \
echo \
echo \
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
echo -----------------
echo.
echo.
echo.
echo |
echo |
echo |
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
echo -----------------
echo.
echo.
echo.
echo /
echo /
echo /
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
echo -----------------
echo.
echo.
echo.
echo.
echo ---
echo.
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
pause
The | character is special to the command interpreter and must be escaped with a ^. You want to use this instead:
echo ^|
Here is what I was talking about regarding the animation labels. For simplicity I used two frames, but the proof of concept is what's important.
#echo off
title Spin
color 0a
echo.
echo -----------------------------------
echo Press a key to make the line spin!
echo -----------------------------------
echo.
echo.
pause >nul
cls
:Animate
Call :Frame1
Call :Frame2
Call :Frame1
Call :Frame2
echo Spin complete!
pause>nul
exit
:Frame1
echo -----------------
echo.
echo.
echo.
echo.
echo ---
echo.
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
GOTO:EOF
:Frame2
echo -----------------
echo.
echo.
echo.
echo ^|
echo ^|
echo ^|
echo.
echo.
echo.
echo.
echo ------------------
ping localhost -n 1 >nul
cls
GOTO:EOF

Resources