Reputation: 3
I need to use one variable content which is in string format, as a name to other variable and call that variable content.
something like:
set upd1= c:\test1
set testval=upd1
set upd=%testval%
echo %upd%
result is upd1 and I would like to get result to show c:\test1
Upvotes: 0
Views: 72
Reputation: 79983
@ECHO OFF
SETLOCAL
set upd1= c:\test1
set testval=upd1
::
:: First way - if value may not begin "="
::
FOR /f "tokens=1*delims==" %%i IN ('set %testval%') DO IF /i "%%i"=="%testval%" SET upd2=%%j
ECHO(first way+%upd2%+
ENDLOCAL
SETLOCAL
set upd1=x====== c:\test1
set testval=upd1
::
:: Second way - value may begin "="
::
FOR /f "delims=" %%i IN ('set %testval%') DO ECHO %%i|FINDSTR /i /b /l /c:"%testval%=" >nul&IF NOT ERRORLEVEL 1 SET upd2=%%i
CALL SET upd2=%%upd2:*%testval%=%%
SET upd2=%upd2:~1%
ECHO(second way+%upd2%+
SET upd
GOTO :EOF
Here's two ways depending on your circumstances. Note I've changed the name upd
to upd2
for easy comparison using the final set
command. Also that your original value of upd1
contains a space; hence I've added a +
to visually delimit the value echo
ed.
Upvotes: 0
Reputation: 3139
@echo off
setlocal enabledelayedexpansion
set name=foo
set foo=bar
echo !%name%!
Echoes "bar".
Upvotes: 1
Reputation: 37569
try this:
@ECHO OFF &SETLOCAL
set "upd1= c:\test1"
set "testval=upd1"
set "upd=testval"
ECHO %upd%
CALL ECHO %%%upd%%%
CALL CALL ECHO %%%%%%%upd%%%%%%%
output:
testval
upd1
c:\test1
Upvotes: 1