referencing a variable in batch file

October 11, 2010 at 06:34:35
Specs: Windows XP, Centrino Duo/1Gb
I am referring to a value stored in variable named "ip1"
I am altering which ip i am calling with a varibale 'x' and trying to call the original value via the following bit in the string "%ip%x%%" but of course it doesn't work. have tried "!ip%x%!" but no joy, am i missing something REALLY simple?

can anyone hellp me please? :( am very new to batch files past basic lines.


See More: referencing a variable in batch file

Report •

#1
October 11, 2010 at 07:31:52
Difficult, at best, to troubleshoot a script without seeing it.


=====================================
Helping others achieve escape felicity

M2


Report •

#2
October 11, 2010 at 08:04:05
can you help me by lettign me know if you can refer to a variable by combining other variables, or the way to do it please:

I have tried to use %IP%x%% for getting access to variable %IP1%, but does not work, can you advise if there is special characters I am missing please.

I also tried making the "IP" bit a variable too and calling that with %%IP%%x%% to no avail.

Been searching for solution for a few hours now and am running out of resources :(

I need it for simple switch network testing.


Report •

#3
October 11, 2010 at 09:37:58
Did you issue the command setlocal EnableDelayedExpansion before coding !ip%x%!?

By the way M2Go is as usual right; you may be familiar to ESP people, but to read others' mind is not a common facility.


Report •

Related Solutions

#4
October 11, 2010 at 21:20:21
Try using the variable %%ip%x%%%

Can I put a question here?


Report •

#5
October 13, 2010 at 04:17:22
OK, here is a cut down version:

ECHO OFF
ECHO START >> LOOPLOG.LOG
ECHO The Date is >> LOOPLOG.LOG
Date /T >> LOOPLOG.LOG
ECHO The Time is >> LOOPLOG.LOG
Time /T >> LOOPLOG.LOG

SET IP1=192.168.11.11
SET IP2=192.168.11.40
SET IP3=192.168.12.11
SET IP4=192.168.12.40
SET IP5=192.168.12.50
SET x=1

:AGAIN
ECHO Checking Connections, Please wait...
ECHO Checking IP%x%
PING -n 1 %%ip%x%%%|find "Reply from " >NUL
IF NOT ERRORLEVEL 1 ECHO IP%x% OK >> LOOPLOG.LOG
IF ERRORLEVEL 1 ECHO IP%x%FAIL >> LOOPLOG.LOG
SET x=%x%+1

IF NOT %x%>79 goto :AGAIN
goto :END

:END
ECHO END >> LOOPLOG.LOG
ECHO The Date is >> LOOPLOG.LOG
Date /T >> LOOPLOG.LOG
ECHO The Time is >> LOOPLOG.LOG
Time /T >> LOOPLOG.LOG

you can see i'm new to this as the x=x+1 bit is wrong too i think but can find that on my own i hope, or if you can answer that too i would be grateful :)

Thanks everyone for their time, sorry for the ESP request to begin with


Report •

#6
October 13, 2010 at 04:26:34
The set command can be used for math with the /a switch.

SET /a x+=1

This would increase x by one.

For more information, at the prompt, type SET /?

Can I put a question here?


Report •

#7
October 13, 2010 at 05:53:14
Cut down further:

====================================

@echo off & setLocal EnableDELAYedeXpansion

SET IP1=192.168.11.11
SET IP2=192.168.1.1
SET IP3=192.168.12.11
SET IP4=192.168.12.40
SET IP5=192.168.12.50
SET X=1

:AGAIN
echo Checking IP%X%

PING -n 1 !IP%X%! | find "Reply from "

IF NOT ERRORLEVEL 1 ECHO IP%X% OK
IF ERRORLEVEL 1 ECHO IP%X%FAIL
SET /a X+=1

IF %X% lss 4 goto :AGAIN


=====================================
Life is too important to be taken seriously.

M2


Report •

#8
October 14, 2010 at 00:53:18
m2, THANK YOU!!

It's working now, was exactly what I was after.

added in some more bits but great to see how it works in some cases now.

Thank you to everyone else too for your input :)

i'll be back :P


Report •

Ask Question