How do I compare variables?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Robuck
Posts: 1
Joined: 14 Jul 2017 19:19

How do I compare variables?

#1 Post by Robuck » 14 Jul 2017 19:25

Hello! I am sort of new to msdos coding, and I am trying to set the color a different color based on the average value of something. I have the average value calculator thing sorted out, but I'm having trouble figuring out how to make it so that if the value is above 0 it turns green, and when below, it turns red. I want it to change the entire screen color scheme, by using the command "color". How do I compare two variables? Do I use the "if" command? if so what do I do? Sorry for being such a helpless noob ;-;

penpen
Expert
Posts: 1996
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How do I compare variables?

#2 Post by penpen » 15 Jul 2017 04:12

Hi there Robuck; you are right: Comparison is performed with the if command.
You may open a command shell ("cmd.exe"), and execute "if /?" for more help.

This might help you:

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
set "variable=0"
call :setColor
pause

set "variable=-1"
call :setColor
pause

set "variable=1"
call :setColor
pause

goto :eof


: setColor
if %variable% gtr 0 (
   color 27
) else if %variable% lss 0 (
   color 47
)
goto :eof


penpen

Post Reply