Use a variable to call a variable preiously set.

  • Thread starter Thread starter Richhall
  • Start date Start date
R

Richhall

Guest
Hi

I want to be able to use a user defined variable to then call a
previously set variable based on the value entered by the user. Is
this possible?

e.g I have variables

set IP0 xxx.xx.xx.xxx
set IP1 xxx.xx.xx.xxx
set IP2 xxx.xx.xx.xxx
set IP3 xxx.xx.xx.xxx
set IP4 xxx.xx.xx.xxx

If the user is prompted for a number

set /p NO=Enter Workstation Number: - user enters 4

set TEMP=IP%NO% - so this is now IP4

How do I now get it to use variable IP4?

i.e
copy \\%IP4%\\logs .........

As if I do copy \\%temp%\\logs it goes to IP4 not the IP address.

Cheers

Rich
 
Re: Use a variable to call a variable preiously set.

I can now get the IP address into a text file:

set var=%%IP%NO%%%
call echo %var% >temp.txt - if I use 4 at the entry prompt this now
uses %IP4% and puts the IP into a text file. Rather than put to a file
can I set this as a variable straight away?

I have looked through the groups but can't understand what I'm meant
to do to get this set as a variable or just to use the text from the
file?
 
Re: Use a variable to call a variable previously set.

Re: Use a variable to call a variable previously set.


"Richhall" <rje.hall@yahoo.co.uk> wrote in message
news:558253a2-80fc-4909-b064-93d02c59a832@k30g2000hse.googlegroups.com...
> Hi
>
> I want to be able to use a user defined variable to then call a
> previously set variable based on the value entered by the user. Is
> this possible?
>
> e.g I have variables
>
> set IP0 xxx.xx.xx.xxx
> set IP1 xxx.xx.xx.xxx
> set IP2 xxx.xx.xx.xxx
> set IP3 xxx.xx.xx.xxx
> set IP4 xxx.xx.xx.xxx
>
> If the user is prompted for a number
>
> set /p NO=Enter Workstation Number: - user enters 4
>
> set TEMP=IP%NO% - so this is now IP4
>
> How do I now get it to use variable IP4?
>
> i.e
> copy \\%IP4%\\logs .........
>
> As if I do copy \\%temp%\\logs it goes to IP4 not the IP address.
>
> Cheers
>
> Rich
>


I think you forgot the "=" signs in your "set" assignments. To get the
selected IP address into a variable, try the following code. In the usual
tradition of advanced batch files, it is highly intuitive (or perhaps the
exact opposite . . .):
@echo off
set IP0=xxx.xx.xx.xx0
set IP1=xxx.xx.xx.xx1
set IP2=xxx.xx.xx.xx2
set IP3=xxx.xx.xx.xx3
set IP4=xxx.xx.xx.xx4

set /p num=Enter a number between 0 and 4:
for %%a in (%num%) do call set MyIP=%%IP%%a%%
echo The selected IP address is %MyIP%
 
Re: Use a variable to call a variable previously set.

Re: Use a variable to call a variable previously set.

excellent, thank you again Pegasus.
 
Re: Use a variable to call a variable previously set.

Re: Use a variable to call a variable previously set.

I also had this suggested that worked in the msdos.batch group.

setlocal enabledelayedexpansion
echo IP for Workstation %no% is !ip%no%!
 
Re: Use a variable to call a variable previously set.

Re: Use a variable to call a variable previously set.


"Richhall" <rje.hall@yahoo.co.uk> wrote in message
news:f42d7afa-fe20-465b-9a91-a3d60312c5bc@t54g2000hsg.googlegroups.com...
>I also had this suggested that worked in the msdos.batch group.
>
> setlocal enabledelayedexpansion
> echo IP for Workstation %no% is !ip%no%!
>


When you multi-post your questions in different newsgroups then you cause
duplication of effort, which considerably reduces your popularity level
among respondents . . . Use cross-posting instead - see here:
http://www.blakjak.demon.co.uk/mul_crss.htm
 
Re: Use a variable to call a variable previously set.

Re: Use a variable to call a variable previously set.

Apologies, won't do it again!

Can you tell me if its possible to set validation on a set /p field
please? i.e only allow ws no entries between 0 and 4?

Cheers, Rich
 
Re: Use a variable to call a variable previously set.

Re: Use a variable to call a variable previously set.


"Richhall" <rje.hall@yahoo.co.uk> wrote in message
news:0d416794-9a5b-47b1-bbd4-e3d9a620294b@z66g2000hsc.googlegroups.com...
> Apologies, won't do it again!
>
> Can you tell me if its possible to set validation on a set /p field
> please? i.e only allow ws no entries between 0 and 4?
>
> Cheers, Rich


You would have to code it like so:
@echo off
:again
set IP=
set /p IP=Please enter a number between 0 and 4:
if "%IP%"=="" goto :eof
for /L %%a in (0,1,4) do if "%IP%"=="%%a" goto Continue
echo You entered %IP%. Why?
goto :again

:Continue
echo You selected %IP%.
 
Re: Use a variable to call a variable previously set.

Re: Use a variable to call a variable previously set.

thank you again
 
Back
Top