haazard
haazard

Reputation: 3

Windows batch using variable content as another vatiable name

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

Answers (4)

Magoo
Magoo

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 echoed.

Upvotes: 0

@echo off
setlocal enabledelayedexpansion

set name=foo
set foo=bar
echo !%name%!

Echoes "bar".

Upvotes: 1

Endoro
Endoro

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

gokhans
gokhans

Reputation: 210

you should change

set testval=upd1

as set testval=%upd1%

Upvotes: 2

Related Questions