I have a batch file that asks the user to input a specific number. If the number is in between a given set of numbers, it goes to that label. However, say the user puts 100 it goes right to :smallsip
All in all, I'm trying to make it so that if the user types a number within a specific range (I.E 30-99) it goes to a specific label. Any suggestions?
:getadrink
cls
echo How many sips will Jackie Chan drink?
set /p numberofsips=Type Number of Sips Here:
if %numberofsips% LSS 0 goto waitwhat
if %numberofsips% GEQ 1 goto smallsip
if %numberofsips% GEQ 10 goto plenty
if %numberofsips% GEQ 30 goto toomuch
if %numberofsips% GEQ 100 goto waytoomuch
:waitwhat
cls
echo what
pause
:smallsip
cls
echo small sips
pause
:plenty
cls
echo plenty
pause
:toomuch
cls
echo too much!
pause
:waytoomuch
cls
echo WAY TOO MUCH
pause
P.S. I've been lurking around numerous posts on here, getting help for something I'm creating with Batch. Yes, I know batch is outdated, but I just seem to like it as I have discovered it about 2 months ago.
The if statements in your code work correctly, but your logic is wrong. For instance, when you type a number 50, the condition %numberofsips% GEQ 1 is already met, so the following if statements will never be reached. To solve this, simply reverse their order.
Another problem is, that you fall into fall into code which you do not want to be executed. For example, when the portion :smallsip has finished (and you confirmed the pause), execution continues at :plenty unintentionally. To avoid that, you will need a goto to jump somewhere else or an exit /B to leave the batch script.
Here is a fixed code:
:getadrink
cls
echo How many sips will Jackie Chan drink?
:askforsips
set numberofsips=0
set /p numberofsips=Type Number of Sips Here:
if %numberofsips% GEQ 100 goto waytoomuch
if %numberofsips% GEQ 30 goto toomuch
if %numberofsips% GEQ 10 goto plenty
if %numberofsips% GEQ 1 goto smallsip
goto waitwhat
:waitwhat
cls
echo what?
pause
goto askforsips
:smallsip
cls
echo small sips
pause
exit /B
:plenty
cls
echo plenty
pause
exit /B
:toomuch
cls
echo too much!
pause
goto askforsips
:waytoomuch
cls
echo WAY TOO MUCH
pause
goto askforsips
These are the things that I changed:
the order of if queries is reversed;
the if %numberofsips% LSS 0 query is removed, so :waitwhat is executed if the entered value is zero or less; in your code, :waitwhat was also executed in case the value was zero as none of the conditions were met; the final (lonely) goto waitwhat is not required here, but it is more obvious what happens;
a new label :askforsips is introduced to allow another user input in case an invalid value (zero or less) was given;
variable numberofsips is now reset before the user prompt, because set /P keeps the former value if the user just presses ENTER;
every section from :waitwhat down to :waytoomuch is terminated explicitly, either by goto askforsips or by exit /B;
Related
Here is the issue. I've been developing some things on batch and use the if command frequently. I've been wanting to have it so that if a variable is equal to a number within a range, then continue the command. This is an example of what I've been doing:
:EXAMPLE
If %variable1% LSS 100 goto fun1
If %variable2% GEQ 100 goto fun2
Echo filler.
Pause
:fun1
Echo filler
Pause
:fun2
If %variable2% GEQ 900 goto fun3
Echo filler
Pause
:fun3
Echo filler
Pause
It works just fine, but I was wondering if there was a faster way to do so like if %variable% == 100-899 goto fun3.
I'm not sure it exists but if you have a solution please let me know. Thanks.
As #user12431753 had touched upon, I have found that
if %variable1% GEQ 300 (if %variable1% LSS 899 echo Hi)
is the most effective way to set a range.
I think I know where the problem is, but not what it is. I isolated it to one line, but that doesn't mean it's the right one.
:CreateProtocol1
cls
echo.
echo How many sides should die %DieCount% have?
echo.
set /p NumberOfSides=/
if %NumberOfSides% gtr 0 (echo set NumberOfSides%DieCount%=%NumberOfSides%) >> Temp.bat
if %NumberOfSides% leq 0 set ErrorMessage=InvalidProtocolCP1
if %NumberOfSides% leq 0 goto ErrorHandler
:CreateProtocol2
cls
echo.
echo How many times should die %DieCount% be rolled?
echo.
set /p NumberOfRolls=/
if %NumberOfRolls% gtr 0 (echo set NumberOfRolls%DieCount%=%NumberOfRolls%) >> Temp.bat
if %NumberOfRolls% leq 0 set ErrorMessage=InvalidProtocolCP2
if %NumberOfRolls% leq 0 goto ErrorHandler
if %DieCount% equ %NumberOfDice% goto RunProtocol
if %NumberOfRolls% gtr 0 set /a DieCount +=1 (<< This seems to be the problem line)
goto CreateProtocol1
This code is meant to roll various amounts of dice for any reason. This is not the full code, just the part that's giving me trouble right now. It says something about "Protocols" before immediately closing so I can't read the rest of the error, usual batch stuff. Even more strangely, it only misbehaves on that line when it's the last "die," if I'm rolling 5, it acts up on the 5th. If I do 9, it acts up on the 9th. Since it rolls all at once, that means I can't see any of the ones I roll. So my question is, what am I doing wrong, and how can I correct it?
I am making a text based rpg and the script is not working. It has something to do with the stamina. I also have a quest that is dependent on you having a certain amount of gold and that script is not working as well. I will include pictures.
:harvest
cls
echo Press 1) to harvest
set /p input17=enter:
if %input17%==1 set /a wheat= %wheat% + 5
if %input17%==1 set /a carrots= %carrots% +4
if %input17%==1 set /a stamina= %stamina% - 25 (this line)
if %stamina% < 0 goto nostamina (this line)
echo.
echo You get some wheat and some carrots.
echo.
echo check your inventory for accurate numbers.
echo.
echo Press 1) to go back.
pause >nul
goto insidehouse
:insidehouse
cls
echo You are now inside of your house.
echo.
echo Press 1) to harvest.
echo Press 2) to sell all crops.
echo Press 3) to go into your inventory.
echo Press 4) to sleep eight hours.
echo Press 5) to check for quests.
set /p input16=enter:
if %input16% EQU 1 goto harvest
if %input16% EQU 2 goto market
if %input16% EQU 3 goto Inventory1
if %input16% EQU 4 goto sleep
if %input16% EQU 5 (and) if %gold% LSS 0 goto shopping (this line)
You haven't provided much code to work with so, I can only guess at a solution.
My best guess is that you are atempting to update a variable from inside a for loop. If this is the case you need to add this line to the top of your batch file: setlocal enabledelayedexpansion. You will also need to access the affected variables like this !var! instead of this %var%.
setlocal enabledelayedexpansion causes expansion of variables to delayed in your batch file. What this will mean in the context of your program is that variables can be updated from within a for loop.
I created a batch file that selects a random playing card using the variable %random%. the batch script keeps looping until %random% is between 1 and 13, and does it again for the suit. I then convert the variables that are set as numbers into text, so the batch file would say something like this: Four of Hearts, or King of Spades. The only problem is, it takes forever! If you open CMD.exe and type in
Echo %random%
and do that until the number is between 1 and four, you will be there FOREVER. Even if you put it into a batch file and use a loop, It still takes about twenty seconds. Is there a way to cut down the time, by perhaps narrowing down the search BEFORE the random number generation? I'm really not sure. In case it helps, or you can't understand me, here's the code:
#echo off
:loop
cls
echo generating card...
set num=%random%
if /i %num% GTR 13 goto loop
if /i %num% LSS 1 goto loop
goto next
:next
set suit=%random%
if /i %suit% GTR 4 goto next
if /i %suit% LSS 1 goto next
goto next2
:next2
if %num%==11 set num=jack
if %num%==12 set num=queen
if %num%==13 set num=king
goto next3
:next3
if %suit%==1 set suiter=Hearts
if %suit%==2 set suiter=Diamonds
if %suit%==3 set suiter=Clubs
if %suit%==4 set suiter=Spades
goto ech
:ech
echo %num% of %suiter%
echo.
echo.
echo.
echo press any key to choose again
pause >nul
goto loop
You can use a little math to get a number between 1 and 13 on the first try:
set /a num=13*%random%/32768+1
and the same thing for 1 and 4
set /a suit=4*%random%/32768+1
See How to use random in BATCH script? - very similar to what you're asking
You can completely eliminate the search using a simple remainder operation, thus limiting the generated random numbers to be in the range [1,13] and [1,4]. This will also simplify the code much. You can achieve this using the 'set /a' command which can be used for arithmetic.
Just change your script to read:
#echo off
:loop
cls
echo generating card...
set /a num=%random% %% 13 + 1
set /a suit=%random% %% 4 + 1
if %num%==11 set num=jack
if %num%==12 set num=queen
if %num%==13 set num=king
if %suit%==1 set suiter=Hearts
if %suit%==2 set suiter=Diamonds
if %suit%==3 set suiter=Clubs
if %suit%==4 set suiter=Spades
goto ech
:ech
echo %num% of %suiter%
echo.
echo.
echo.
echo press any key to choose again
pause >nul
goto loop
Good luck!
My batch file always returns PLUGINS instead of going to the desired place when I press any number between those prompted. What could the problem stem from?
#echo off ECHO Control Panel initialized.
:BEGIN
CHOICE /N /C:1234 /M "PICK A NUMBER (1(PLUGINS), 2(MOTD), 3(LOGS),
4(END)"%1
IF %%ERRORLEVEL ==1 GOTO ONE
IF %%ERRORLEVEL ==2 GOTO TWO
IF %%ERRORLEVEL ==3 GOTO THREE
IF %%ERRORLEVEL ==4 GOTO END
:ONE
ECHO PLUGINS
explorer \\192.168.1.16\Server\Server-Dedicated Slightly\Here is the
Actual Server dir\plugins
GOTO BEGIN
:TWO
ECHO MOTD
notepad \\192.168.1.16\Server\Server-Dedicated Slightly\Here is the
Actual Server dir\plugins\Essentials\motd
GOTO BEGIN
:THREE
ECHO LOGS
notepad \\192.168.1.16\Server\Server-Dedicated Slightly\Here is the
Actual Server dir\server
GOTO BEGIN
:END
PAUSE
Three problems:
ERRORLEVEL checks for the given value, or above, so you have to order them from HIGH to LOW, not the other way around
it's ERRORLEVEL, not %%ERRORLEVEL
it's just ERRORLEVEL num
So, in short, you'll get
IF ERRORLEVEL 4 GOTO END
...
IF ERRORLEVEL 1 GOTO ONE