Can a batch file tell if a number is bigger or smaller than another number
the reason i need to find this out is for high scores on a batch file game
the batch file stores the high score in a text file
reads the text file when ever the file is run
the batch file ovewrites the score wether its higher or lower
any suggestions? any help will be greatly appreciated.
thanks in advance--=BatMaster=--
can a batch tell if a number is bigger or smaller than anoth
Moderator: DosItHelp
Re: can a batch tell if a number is bigger or smaller than a
Code: Select all
@echo off
set n1=3
set n2=5
if %n1% lss %n2% (
echo smaller
) else if %n1% gtr %n2% (
echo bigger
) else if %n1% equ %n2% (
echo equal
)
pause
Re: can a batch tell if a number is bigger or smaller than a
More importantly - this works
because IF sorts the values as numeric values as long as they contain nothing but digits.
If either value contains at least one character that is not a digit then the values are sorted alphabetically instead:
For example, 9 would be greater than 10x
Dave Benham
Code: Select all
set score=10
set hiScore=9
if %score% gtr %hiScore% (
set hiScore=%score%
echo New high score = %hiScore%
)
If either value contains at least one character that is not a digit then the values are sorted alphabetically instead:
For example, 9 would be greater than 10x
Dave Benham
Re: can a batch tell if a number is bigger or smaller than a
@dbenham your cose doesnt change the value of hiscore even if score=123 and hiscore=9
@batcher your code works perfectly i wil pm you the code for the game soon
regards--=BatMaster=--
@batcher your code works perfectly i wil pm you the code for the game soon
regards--=BatMaster=--
Re: can a batch tell if a number is bigger or smaller than a
@dbenham
i fixed your code
--=BatMaster=--
i fixed your code
Code: Select all
@echo off &setlocal
set score=10
set hiScore=9
if %score% lss %hiScore% goto end
set hiScore=%score%
echo New high score = %hiScore%
pause
:end
exit
--=BatMaster=--
Re: can a batch tell if a number is bigger or smaller than a
Thanks! I made a rookie mistake. You can't see changes to variables within a loop using immediate expansion
My code would work fine if I used delayed expansion:
Your fix to my code is also a good solution
Dave Benham
My code would work fine if I used delayed expansion:
Code: Select all
setlocal enableDelayedExpansion
set score=10
set hiScore=9
if %score% gtr %hiScore% (
set hiScore=!score!
echo New high score = !hiScore!
)
Your fix to my code is also a good solution
Dave Benham