Batch file. Programming. Comparisons. Greater than less than. if...then... nested for loops.

Here's the background to my current situation.  I've got 8 temperamental  adsl lines, 30 computers.  This batch file adds all the routers to the local machine as persistent routes and dns servers:
  route DELETE 0.0.0.0
  route ADD 0.0.0.0 MASK 0.0.0.0 192.168.0.1 METRIC 1 -p
  route ADD 0.0.0.0 MASK 0.0.0.0 192.168.0.2 METRIC 2 -p
  route ADD 0.0.0.0 MASK 0.0.0.0 192.168.0.3 METRIC 3 -p
  route ADD 0.0.0.0 MASK 0.0.0.0 192.168.0.4 METRIC 4 -p
  route ADD 0.0.0.0 MASK 0.0.0.0 192.168.0.5 METRIC 5 -p
  route ADD 0.0.0.0 MASK 0.0.0.0 192.168.0.6 METRIC 6 -p
  route ADD 0.0.0.0 MASK 0.0.0.0 192.168.0.7 METRIC 7 -p
  route ADD 0.0.0.0 MASK 0.0.0.0 192.168.0.8 METRIC 8 -p
  netsh interface ip DELETE dns LOCAL ALL
  netsh interface ip ADD dns LOCAL 192.168.0.1 index=1
  netsh interface ip ADD dns LOCAL 192.168.0.2 index=2
  netsh interface ip ADD dns LOCAL 192.168.0.3 index=3
  netsh interface ip ADD dns LOCAL 192.168.0.4 index=4
  netsh interface ip ADD dns LOCAL 192.168.0.5 index=5
  netsh interface ip ADD dns LOCAL 192.168.0.6 index=6
  netsh interface ip ADD dns LOCAL 192.168.0.7 index=7
  netsh interface ip ADD dns LOCAL 192.168.0.8 index=8
  ipconfig /flushdns

If I run this batch file on every computer, they all start using the same ADSL line.  It does achieve half of it’s objective in that when 192.168.0.1 goes down, all computers start using 192.168.0.2, and then 192.168.0.3.  Now, rather than write 8 different batch files, I want to determine the IP address of the computer and then assign metrics according to which range that ip address is in.  All clients have ips between 192.168.0.11 and 192.168.0.45
Hence the following 8 groups:

  IP           Metrics/Indexes for routers 1 to 8.
  11-15     1,2,3,4,5,6,7,8
  16-20     2,3,4,5,6,7,8,1
  21-25     3,4,5,6,7,8,1,2
  26-30     4,5,6,7,8,1,2,3
  31-35     5,6,7,8,1,2,3,4
  36-40     6,7,8,1,2,3,4,5
  41-45     7,8,1,2,3,4,5,6
  46-50     8,1,2,3,4,5,6,7

The following dos command from within a batch file sets %IPADDRESS% to the last octet. i.e. 11 or 12 or 13:
  for /F "tokens=1-6 delims=:. " %%a in ('ipconfig ^| find "IP Address"') do set IPADDRESS=%%f

After this I get stuck, do I create lists for each of the different metrics?
How do I do a comparison with  %IPADDRESS% to define which list to use?
And then how do I execute
  route ADD 0.0.0.0 MASK 0.0.0.0 192.168.0.1 METRIC 1 –p
  netsh interface ip ADD dns LOCAL 192.168.0.1 index=1
with the metrics from the list based on the %ipaddress%

if you need anything clarified let me know!

ps. please don't suggest linux load-balacing proxy servers that do the same thing, i want to do it all through windows. Also, we're on the edge of a telephone exchanges' catchment area, that's why we've got 8 lines that go down so often and we can't get fibre without a £20,000 install.
jondangerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DrWarezzCommented:
Hi,
you're going to want to use the nested IF statements, like so:

for /F "tokens=1-6 delims=:. " %%a in ('ipconfig ^| find "IP Address"') do set IPADDRESS=%%f

:** If the Last Octet is greater than or equal to 46 & it is less than or equal to 50, then ...
IF %IPADDRESS% GEQ 46 ( IF %IPADDRESS% LEQ 50  ECHO [Range: 46-50], do something here )

:** If the Last Octet is greater than or equal to 41 & it is less than or equal to 45, then ...
IF %IPADDRESS% GEQ 41 ( IF %IPADDRESS% LEQ 45  ECHO [Range: 41-45], do something here )

:** Etc ...


Does that help at all?
If the task you want to perform is loads of lines long, then you may want to do something like this:


for /F "tokens=1-6 delims=:. " %%a in ('ipconfig ^| find "IP Address"') do set IPADDRESS=%%f

:** If the Last Octet is greater than or equal to 46 & it is less than or equal to 50, then ...
IF %IPADDRESS% GEQ 46 ( IF %IPADDRESS% LEQ 50  GOTO 46_50 )

:** If the Last Octet is greater than or equal to 41 & it is less than or equal to 45, then ...
IF %IPADDRESS% GEQ 41 ( IF %IPADDRESS% LEQ 45  GOTO 41_45 )

:** Etc ...

GOTO :EOF

:46_50
ECHO Range is 46-50, do something here...
ECHO ... and here ...

GOTO :EOF

:41_45
ECHO Range is 41-45, do something here...
ECHO ... and here ...

GOTO :EOF

:EOF
:** The end here, to any cleanup stuff necessary ...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SteveGTRCommented:
Try this:

@echo off

setlocal enabledelayedexpansion

set debug=
REM Remark this line to activate processing
set debug=echo

for /F "tokens=1-6 delims=:. " %%a in ('ipconfig ^| find "IP Address"') do set IPADDRESS=%%f

set /A index=(%IPADDRESS% - 1) / 5

REM <16 (11-15)
if /I %index% LSS 3 set array=1,2,3,4,5,6,7,8
REM 16-20
if /I %index% EQU 3 set array=2,3,4,5,6,7,8,1
REM 21-25
if /I %index% EQU 4 set array=3,4,5,6,7,8,1,2
REM 26-30
if /I %index% EQU 5 set array=4,5,6,7,8,1,2,3
REM 31-35
if /I %index% EQU 6 set array=5,6,7,8,1,2,3,4
REM 36-40
if /I %index% EQU 7 set array=6,7,8,1,2,3,4,5
REM 41-45
if /I %index% EQU 8 set array=7,8,1,2,3,4,5,6
REM >45 (46-50)
if /I %index% GTR 8 set array=8,1,2,3,4,5,6,7

%debug% route DELETE 0.0.0.0

set /A cnt=1

for %%a in (%array%) do (
  %debug% route ADD 0.0.0.0 MASK 0.0.0.0 192.168.0.!cnt! METRIC %%a -p
  set /A cnt+=1
)

%debug% netsh interface ip DELETE dns LOCAL ALL

set /A cnt=1

for %%a in (%array%) do (
  %debug% netsh interface ip ADD dns LOCAL 192.168.0.!cnt! index=%%a
  set /A cnt+=1
)

This code has the debug process enabled. To enable it you should remark the 2nd set debug command at the top of the batch file (as noted in remark). Also,  I'm not sure if I got your processing correct in terms of the route and netsh commands so check that out.

Good Luck,
Steve
0
jondangerAuthor Commented:
Thanks guys, both solutions worked but DrWarez, i've implemented yours. I'll split the points evenly.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

jondangerAuthor Commented:
i did well there didn't i? how do u split points?
0
SteveGTRCommented:
Go to http://www.experts-exchange.com/Community_Support/ and post a message stating that you didn't split the points as you intended. You might also consider asking them to change the grade to an A. In my eyes, there is no reason for the B.

Thanks :)
0
DrWarezzCommented:
:-)
0
DrWarezzCommented:
Thanks. :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.