[Question] Check if a variable is between numbers

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

[Question] Check if a variable is between numbers

#1 Post by Ranguna173 » 11 Nov 2011 14:44

Hello!

My question is if it is possible to see if a variable is between two numbers. Ex:.
I have a variable called Num1 and it contains a random number.
Now I want to now if that number is between 1 and 5.
How do I do that?


Pleas help and thanks!

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: [Question] Check if a variable is between numbers

#2 Post by dbenham » 11 Nov 2011 15:02

Code: Select all

if %Num1% geq 1 if Num1 leq 5 echo %Num1% is between 1 and 5

This is pretty basic stuff. Read up on IF (HELP IF)

Dave Benham

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: [Question] Check if a variable is between numbers

#3 Post by Ed Dyreen » 11 Nov 2011 15:02

'

Code: Select all

@echo off

set $var=hello &call :§scope
set $var=10 &call :§scope
set $var=3 &call :§scope

pause
exit /b 0

:§scope ()
::(
   if defined $var (
      if %$var% equ +%$var% (
         if %$var% lss 5 (
            if %$var% gtr 0 (
               echo.variable matches scope.
            )
         )
      )
   )
::)
exit /b 0
a macro would be easier but that may be too complex...

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: [Question] Check if a variable is between numbers

#4 Post by dbenham » 11 Nov 2011 15:09

Ed Dyreen wrote:

Code: Select all

...
         if %$var% lss 5 (
...
Should read either if %$var% lss 6 (
or if %$var% leq 5 (

Ed Dyreen wrote:a macro would be easier but that may be too complex...
:shock: :? :lol: classic :!:

Dave Benham

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: [Question] Check if a variable is between numbers

#5 Post by Ranguna173 » 11 Nov 2011 15:14

The creation of this topic is my ignorance.
When I was reading the "if /?" i skipped the "gtr" "lss" "equ"..etc. section..

I'm sorry!
And thanks for your help.

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: [Question] Check if a variable is between numbers

#6 Post by Ranguna173 » 11 Nov 2011 15:18

Ed Dyreen wrote:'
]a macro would be easier but that may be too complex...


dbenham wrote:
Ed Dyreen wrote:[code]
Ed Dyreen wrote:a macro would be easier but that may be too complex...
:shock: :? :lol: classic :!:

Dave Benham


Wait.. Are you making fun of me?

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: [Question] Check if a variable is between numbers

#7 Post by dbenham » 11 Nov 2011 15:23

No - I was not making fun of you.

I found Ed's statement to be humorous; it is an oxymoron - meaning a statement that contradicts itself.

Dave Benham

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: [Question] Check if a variable is between numbers

#8 Post by Ranguna173 » 11 Nov 2011 15:39

dbenham wrote:it is an oxymoron - meaning a statement that contradicts itself.

Dave Benham


Oh.. I get it.
Maybe the "easy" ment something else or Ed thought that it would be easy for him but to complex for me.. (In which he is wrong 8) )

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: [Question] Check if a variable is between numbers

#9 Post by Ed Dyreen » 11 Nov 2011 16:27

'
I mean macro's are only beneficial if you are going to depend on them multiple times during execution. Otherwise it's a waste of developing time.

Post Reply