Value of the value set to a variable?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Cat
Posts: 32
Joined: 11 Nov 2011 12:04

Value of the value set to a variable?

#1 Post by Cat » 26 Nov 2011 21:57

Here is an example:

Code: Select all

set test=96
set /p 96="random prompt"

say %96% is set to 7. I want to activate a condition for %test% to check the variable of %96%:

Code: Select all

if %%test%%==7 echo 7
if %!test!%==7 echo 7
if !%test%!==7 echo 7

None of the above works. How do i fix this?

Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Re: Value of the value set to a variable?

#2 Post by Rileyh » 26 Nov 2011 22:16

How is your example relevant to the question? And what do you mean
I want to activate a condition set to %test% to check the variable %96%

But I can propose an answer to your question anyway:

Code: Select all

@echo off &setlocal enabledelayedexpansion
set "test=96"
set /p 96=Enter a value:
if /i _"%test%"==_"(whatever value you want to check for)" (do whatever comes to mind)


Or if you want the user to see the value of %test%:

Code: Select all

@echo off
set "test=96"
set /p 96=Something:
echo %test%

Or:

Code: Select all

@echo off
set "test=if /i _"%96%"==_"(whatever value you want to check for)" (do something)"
set /p 96=Something:
%test%


These are all guesses as a result of what your question poses, so they may not work. You need to re-phrase your question.

Regards,
Rileyh

Cat
Posts: 32
Joined: 11 Nov 2011 12:04

Re: Value of the value set to a variable?

#3 Post by Cat » 26 Nov 2011 22:31

To be more specific, here is the exact portion of code:

Code: Select all

:input
set "X%X%Y%Y%= "
choice /c awsd /n >nul
if %errorlevel%==1 (
   if %Y% GTR 1 (
      set /a Y=%y%-1
      ) else (
      goto lose
   )
)
if %errorlevel%==2 (
   if %x% GTR 1 (
      set /a x=%x%-1
   ) else (
      goto lose
   )
)
if %errorlevel%==3 (
   if %X% LSS 17 (
      set /a X=%x%+1
      ) else (
      goto lose
   )
)
if %errorlevel%==4 (
   if %Y% LSS 25 (
      set /a Y=%Y%+1
   ) else (
      goto lose
   )
)
set target=X%X%Y%Y%
if !%target!%==* set /a coinss+=1
X%X%Y%Y%
set X%X%Y%Y%=²
if %coinss% gtr 5 (
   if X%X%Y%Y%==X%f%Y%t% (
      call :coins
      call :coins
   )
)
)
if %coinss% gtr 20 (
   if X%X%Y%Y%==X%f%Y%t% (
      call :coins
      call :coins
      call :coins
   )
)
if X%X%Y%Y%==X%f%Y%t%call :coins
call :grid
goto input

X is set to 12, Y is set to 17
Target is set to X%X%Y%Y%
I want the condition to activate for the variable '%X%X%Y%Y%%', or %X12Y17%.
This is a loop, so it has to work with X and Y being set to different numbers.
Sorry if this is immensely confusing.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Value of the value set to a variable?

#4 Post by !k » 27 Nov 2011 01:29

Cat wrote:None of the above works.
it works for me

Code: Select all

setlocal enabledelayedexpansion
set test=96
set /p 96="random prompt : "
if "!%test%!" == "7" echo Seven
___________
XP SP3 x32

Cat
Posts: 32
Joined: 11 Nov 2011 12:04

Re: Value of the value set to a variable?

#5 Post by Cat » 27 Nov 2011 10:54

it works for me

Code: Select all

setlocal enabledelayedexpansion
set test=96
set /p 96="random prompt : "
if "!%test%!" == "7" echo Seven



That only seems to work with numbers, not text strings:

Code: Select all

setlocal enabledelayedexpansion
set test=cat
set /p 96="random prompt : "
if "!%test%!" == "cat" echo Cat

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Value of the value set to a variable?

#6 Post by Ed Dyreen » 27 Nov 2011 11:12

'
Cat wrote:That only seems to work with numbers, not text strings:

Code: Select all

setlocal enabledelayedexpansion
set test=cat
set /p 96="random prompt : "
if "!%test%!" == "cat" echo Cat
you are referring to cat, not to 96:

Code: Select all

setlocal enabledelayedexpansion
set test=cat
set /p "cat=random prompt : " %= entering 'cat' evals true =%
if "!%test%!" == "cat" echo Cat

Post Reply