Use a variable to call a variable preiously set.

R

Richhall

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
 
R

Richhall

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?
 
P

Pegasus \(MVP\)

Richhall said:
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%
 
R

Richhall

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

setlocal enabledelayedexpansion
echo IP for Workstation %no% is !ip%no%!
 
P

Pegasus \(MVP\)

Richhall said:
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
 
R

Richhall

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
 
P

Pegasus \(MVP\)

Richhall said:
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%.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top