Batch file - "If" command with range - batch-file

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.

Related

Skipping to next step in Batch program

#echo off
setlocal EnableDelayedExpansion
color b
goto play
:play
cls
set name2= OoggieBoogie
echo Hello, My name is !name2!^^! I'm an AI. I'm here to help with your lazy
Butt :D^^!
timeout /t 3 >null
echo!name2!: May I Have Your Have Your Name Please? :)
color c
echo (Pssst^^! Want to cut the Bullshit and go straight in? Select "Express" please!)
timeout /t 2 >null
echo A. My name is
echo B. Express
set /p input=
if !input! equ B goto Writing2
cls
echo!name2!: Hello !name!, Shall we continue now?
echo 1.Yes :D
echo 2.No -_-" ..
set /p input=!name!:
if !input! equ 1 goto Writing
if !input! equ Yes goto Writing
if !input! equ 2 exit
if !input! equ No exit
:Writing2
echo Okay.. Whatever you want Damn.. I was trying to be nice ^^!
echo Anyway. Inatiating EXPRESS Route----->
goto Writing
Hello!
What I am trying to do here is skipping all the steps and go straight to "Writting2" if typed Express.
I am almost done with this fun program but I can't figure out a good way skip all the steps.
When I type "Express or select 'B'
it crashes.
but If I write a name the program works as usual!
Thanks in Advance!
Sorry in Advance if I did something wrong in the community.
This is a way you can do what you want:
#echo off
color b
goto play
:play
cls
set name2= OoggieBoogie
echo Hello, My name is %name2% I'm an AI. I'm here to help with your lazy
::echo Butt :D^^!
timeout /t 3 >nul
echo %name2%: May I Have Your Have Your Name Please? :)
color c
echo (Pssst^^! Want to cut the Bullshit and go straight in? Select "Express" please!)
timeout /t 2 >nul
echo A. My name is
echo B. Express
choice /c:AB>NUL
if errorlevel 2 goto Writing2
set /p "name=Enter your name: "
:Writing
cls
echo %name2%: Hello %name%, Shall we continue now?
echo 1.Yes :D
echo 2.No -_-" ..
choice /c:1Y2N>NUL
if errorlevel 4 goto exit
if errorlevel 3 goto exit
if errorlevel 2 goto Writing
if errorlevel 1 goto Writing
:Writing2
echo Okay.. Whatever you want Damn.. I was trying to be nice ^^!
echo Anyway. Inatiating EXPRESS Route-----^>
goto Writing
:exit
exit /b
Remember: Variables can be accessed by %variable_name% and you can set them by set "variable_name=variable_value" as #Compo mentioned above.
It is better to use choice /c option in your future batchfiles. A disadvantage of this option is that you cannot enter a string with more than 1 character, but it does it's own errorhandling, so you don't have to deal with invalid responses. Also, when you write
echo Anyway. Initiating EXPRESS Route----->
> symbol causes problems as it is a redirection character and should be escaped:
echo Anyway. Initiating EXPRESS Route-----^>

Using Multiple GTR, LEQ, etc "if" statements in Batch

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;

My text based game script is not working

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.

Batch file - Pick a card

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!

Creating a geometrically decaying pause loop

So I am working on creating a batch file game analogious to a "Memory" game. (ie where the player is presented a list of objects for a short time, then asked to repeat the pattern)
My problem comes in how to decrease the time the pattern is exposed to the player as the round # increases.
Here is my current code:
#echo off
set /a y=50
set /a x=1000
:foo
set /a y=%y% + %y%
set /a x= %x% - %y%
echo %y%
echo %x%
ping -n 10 -w %x% 127.0.0.1 > nul
goto foo
When run, the above code does present x and y values that change as expected, however the wait time is always the same. Why is this and how can I fix it?
Thank you for your time.
Firstly why dont you use sleep? It would work fine (type sleep /? for more info)
However, here is another way of doing this with for /l loops
#echo off
setlocal enabledelayedexpansion
set score=0
title Memory Test : Current Score = !score!
for /l %%a in (0,1,20) do (
Rem In the above sequence, increase 20 to the amount of times you want the test to be performed
set number[%%a] = !random!!random!
echo Number: !number[%%a]!
set /a wait=21-%%a
set /a wait=!wait!*1000/4
sleep -m !wait!
cls
set /p "input=What was the last number youy saw? "
if !number[%%a]! equ "!input!" (
set /a score=!score!+1
Echo Correct !
title Memory Test : Current Score = !score!
)else(
Echo Incorrect! Coreect Answer = !number[%%a]!
)
)
echo Calculating score...
pause
cls
echo.
if %score% leq 14 set msg="Nice Try! But you can do better!"
if %score% geq 15 set msg="Good Job! Your on your way to the top!"
if %score% equ 20 set msg="Your So Close! Almost a perfect socre!"
if %score% equ 21 set msg="You got a perfect score! Woderful!"
Echo %score%/21 : %msg%
echo.
pause
And that should work fine. Note you can change how long the test goes for, but for the first game they'll have a bit more then 5 seconds to study the question, and in the last round a quarter of a second!
Mona
In order for the ping/wait trick to work, the ip address must not exist. 127.0.0.1 is your own computer, so it has no opportunity to timeout since the ping response is successful and immediate.
Instead, choose an ip address that doesn't exist. E.g. 10.20.30.40 (assuming that doesn't exist.)
You can adjust the 10 in this to give approximately second resolution. Is that sufficient for your code?
ping -n 10 127.0.0.1 > nul
This is another option for later windows.
timeout /t 10 >nul

Resources