Endless loop in batch file using 'goto' - batch-file

Well, I have this problem with a little program that I'm making because my loop is endless. Right now I'm trying to use goto command but it doesn't work couse it is endless. Anyone please help.
#echo off
color 1a
:i
echo Hi, mate Let's start off by getting each others names.
timeout 5 > nul
echo What is your name ?:
set /p name=
echo Hi noob %name% my name is Youke
timeout 2 > nul
echo %name% We are going on an advenutre quest today :D
timeout 2 > nul
goto i

Somewhere between the i label on line 3 and the goto command, you need to add code that will break out of the loop—likely an if statement with another goto inside it that points to a label after your current goto.
You can see plenty of examples here: https://stackoverflow.com/a/4711984/436282

Related

Why doesnt this code work?

This is my first question so sorry if its a litle vague.
I have been working on a program for a wile now, it is supposed to be a schedule of tasks and the time those tasks have to be completed.
Everything works just fine but i cant figure out why this part doesn't work.
:edit
echo Select Task
echo (from 1 to 7)
set /p sel=
if %sel%=1 ( goto task1 )
And then its supposed to bring me here
:task1
pause
echo Insert Task
echo.
set /p task1=
echo.
echo Set Time
echo.
set /p %time1%=
echo.
pause
It's your IF statement. a single equals sign "=" is used as an assignment operator in batch:
set var=fubar
to compare two values use a double equals "==":
if "%var%"=="fubar" echo It'll be reet!
Hope that helps, this was one of the things that always got me when I was starting to code and you will get used to it.
As #Antonio said, there is a problem with the if statement. I also find another problem in :task1
set /p %time1%=
should be
set /p time1=
...and go to www.ss64.com/nt and take a little lesson of batch scripting.
The line: if %sel%=1 ( goto task1 ) is very incomplete;
Write it so:
if "%sel%"=="1" goto task1

BATCH program crashes after goto command

This code is part of a chat program that I am currently working on. The 'else' part of my program is the one that doesn't work. The program quits instead of going to :home
:join
cls
if not exist "C:/Users/Public/room.cmd" (
echo No room has been found.
echo.
set /p choiceretry=Do you want to retry? y/n
if "%choiceretry%"=="y" goto join
if "%choiceretry%"=="n" goto home
) else (
cls
"C:/Users/Public/room.cmd"
echo A room has been found.
pause >nul
echo Joining
set roomjoined=1
echo %roomjoined%
goto home
)
:home
echo this finally works
pause
I have tried changing the code several times starting from 'echo Joining'
Anyone know why cmd quits?...
:) :) :)
Thanks in advance
The problem is the way you run room.cmd; you must use call to return from it:
call "C:/Users/Public/room.cmd"
Otherwise, execution will not return from room.cmd to the original batch file that ran it.
Hint: Consider to use choice instead of set /P for Y/N decisions.
Firstly, please don't left justify your code blocks. It's much easier to read code that's properly indented.
Secondly, when retrieving values within a code block, you need delayed expansion. See setlocal /? in a cmd prompt for more information. This is the reason for the unexpected behavior. Your variables retrieved within the same parenthetical code block in which they were set won't contain the values you expect unless you retrieve them with delayed expansion syntax. As an alternative, you could use the choice command and if errorlevel, which would result in a bit nicer user experience I think.
Thirdly, when testing user input, you should use the /i switch in your if statements for case-insensitivity. This isn't relevant if using choice / if errorlevel though.
Fourthly, Windows paths use backslashes, not forward slashes.
I'd fix it this way:
#echo off
setlocal
:join
cls
if errorlevel 1 set /P "=Retrying... "<NUL
if not exist "C:\Users\Public\room.cmd" (
echo No room has been found.
echo.
choice /c yn /n /m "Do you want to retry? [y/n] "
if errorlevel 2 goto home
goto join
) else (
"C:\Users\Public\room.cmd"
echo A room has been found.
pause >nul
echo Joining
set roomjoined=1
)
:home
echo this finally works
pause

What is wrong with this batch script

I am starting to get into programming and recently learned the basics of batch script. I am doing something private, but whenever I try to use an input I put to "set/p", it exits the batch.
I'd like to know if there is something wrong with it
set /p msg=
if %msg%== y goto :y1
if %msg%== n goto c1
:y1
cls
color 0a
Before and after this there is just "echo" and text
Thanks in advance
You should use quotes around items when comparing, eg.:
if "%msg%" == "y"
Not at my computer now but four things to check / try.
Use IF /i so that it will work even if you type in CAPS. (http://ss64.com/nt/if.html )
As thebjorn said, its good to use the "" around the variable and the value.
Your example looks like it may be missing a space after the %msg%
The most likely issues based just on the code youve included is what Stephen said... In the example code, once it gets to the :y1 label would clear the screen, set color then do your echo super fast and exit.
Try the pause trick.
Thanks for the answer but I already figured it out.
I tried different changes and then I finally got it
I just needed to capitalize GOTO
#echo off
#echo [question] [message] y/n?
set /p msg=
if %msg%== y GOTO :y1
if %msg%== n GOTO c1
:y1
cls
color 0a
set /p msg=
if %msg%== y goto :y1
if %msg%== n goto :c1
:y1
cls
color 0a
I believe your error would be your beginning. Where you haveset /p msg=, this would have %msg% equaling nothing which if your batch program starts with an error then it will close immediately.
You can have it equal anything you want for example if you want it to be blank you would have to have: set /p msg=" " and in the Command Prompt it will just take your cursor to a blank line waiting for your input.
The most standard option is what command prompt uses to indicate a new line: set /p msg=" > "
I edited the code for you and you can change it as you wish... I cleaned up what it would look like a bit for you also
#echo off
echo input y or n
echo.
set /p "msg= > "
if "%msg%"=="y" goto :y1
if "%msg%"=="n" goto :c1
:y1
cls
color 0a
pause
Edit 1: fixed my quotes in the above code... the original placing wouldn't of done much with the above by itself but with additional code it may cause problems, when it comes to quotes and spaces batch is kinda picky
Side Note 1: if you want to have a timed pause instead of it waiting for a button press when it reaches pause, you can use the command timeout /t # the # would be the number of seconds you want it to wait and you can also press any button to have it be like the pause button... now just like how the pause button has the "Press any key to continue..." phrase so with timeout it has something like this but do not quote me on this "Press any key to continue or wait #..." you can hide the phrases from appearing but using either of these code lines: pause >nul or timeout /t # >nul

If statements not working in batch file

I made this code for something to remind me about certain things I need to do, but for some reason the code isn't working.
#echo off
cls
echo Welcome to ChoreHub
echo -------------------------------------------
echo Today's List:
echo * Do School
echo * Practice Piano
echo * Check to Make Sure House is Clean
echo * Check to Make Sure Room is Clean
echo -------------------------------------------
echo What chore have you finished? (1, 2, 3, 4)
set /P chore=
if %chore%=="1" goto :1
if %chore%=="2" goto :2
if %chore%=="3" goto :3
if %chore%=="4" goto :4
goto bro
When I put in a right answer, it skips over the if statements and goes straight to the bottom.
I'm not sure why, I am a little rusty on batch files though. If anyone could help me that would be great, thanks!
you haven't set anything? where is 1,2,3 and bro? where is it supposed to go??
you need to add this
:1
echo 1 is selected
goto :end
2:
echo 2 is selected
goto :end
:3
echo 3 is selected
goto :end
:end
check this fully working batch example
Hard to tell without seeing all your code, but I think, your problem is here:
if %chore%=="1" goto :1
You are comparing your variable with "1" (not just 1 what you seem to think)
You need to enter "1" ( Quote 1 Quote ) to match this.
Better code:
if "%chore%"=="1" goto :1
(Note: if %chore%==1 seems to be correct too, but will give a syntax error, if %chore% is empty)

How to use a "if" condition properly in batch

Sorry for my bad english expression ... i'm not native.
I have searched for an answer for a while ... maybe i'm not doing this right but it seems that my (simple) code is not working for a reason.
As soon as I open the batch programm it shuts without me having the time to read the error message :(
could some one please correct this one ??
I am grateful for any kind of help !
#echo off
color 0a
mode 1000
title THE OFFSWITCH
:start
cls
if %time%== "12:00:00.00" goto :time
echo.
echo IT IS NOT TIME YET : %time%
goto start
:time
echo hello
pause>nul
It is pure luck, if your code works. In the most cases, it will just miss the time, because the loop is not fast enough to get the correct centisecond.
Instead of
if %time%== "12:00:00.00" goto :time
you should use
if "%time%" geq "12:00:00.00" goto :time
(if time is "greater or equal" than...)
Also you should consider to put a delay into the loop, because it eats all of the CPU-Time, it can get, and thus slows down your system. For example timeout /t 1>nul waits for one second.

Resources