luikstruik
luikstruik

Reputation: 93

Batch variable concatenation for loop

I'm trying to create a for loop, that outputs a certain range of responses.

Using the following works fine:

echo Node1:
dashd-cli masternodelist status 123.123.123.100
echo.
echo Node2:
dashd-cli masternodelist status 123.123.123.200
echo.
echo Node3:
dashd-cli masternodelist status 123.123.123.300

OUTPUT:

Node01: { "123.123.123.100" : "ENABLED" }

Node02: { "123.123.123.200" : "ENABLED" }

Node03: { "123.123.123.300" : "ENABLED" }

I'm now trying to create a for loop that checks through a list of nodes:

cls
@echo off
set nodeCount=7
set Node1="111.111.111.111:1111"
set Node2="222.222.222.222:2222"
set Node3="333.333.333.333:3333"
set Node4="444.444.444.444:4444"
set Node5="555.555.555.555:5555"
set Node6="666.666.666.666:6666"
set Node7="777.777.777.777:7777"

for /L %%C in (1,1,%nodeCount%) do (
echo Node%%C:
dashd-cli masternodelist status %Node1%
)

The above method lists the Node1 IP up to seven times, how can I make it loop through the list of nodes? I'm well aware of the %%C but I simply don't know how to apply it in this case.

Upvotes: 0

Views: 180

Answers (1)

SachaDee
SachaDee

Reputation: 9545

Enable the delayedexpansion and use !node%%C!:

cls
@echo off
setlocal enabledelayedexpansion
set nodeCount=7
set Node1="111.111.111.111:1111"
set Node2="222.222.222.222:2222"
set Node3="333.333.333.333:3333"
set Node4="444.444.444.444:4444"
set Node5="555.555.555.555:5555"
set Node6="666.666.666.666:6666"
set Node7="777.777.777.777:7777"

for /L %%C in (1,1,%nodeCount%) do (
echo Node%%C:
dashd-cli masternodelist status !Node%%C!
)

Upvotes: 1

Related Questions