-1

I have created a batch file that alters the PATH depending on what compiler name I pass to it. I also want to select from a number of these variables at runtime, I have another batch file where I generate the name of the appropriate environmental variable to look at, but I can't see how to expand the generated variable

e.g

EXE_PATH_%_1%_%PLATFORM%

so when the batch file is called the environmental variables _1 and PLATFORM expand to produce sections of the naming convention for the name of the variable I want to expand

if I call echo EXE_PATH_%_1%_%PLATFORM% this would produce the name of the variable I want to expand

How can I go from the generated name, to the contents of the environmental variable with that name?

I suspect delayedexpension is required but I'm not sure

Peter Nimmo
  • 1,045
  • 2
  • 12
  • 25

1 Answers1

0

It is not really clear what you mean, but here is most likely the information you need:

@echo off
setlocal
set "_1=VSC"
set "PLATFORM=x64"
set "EXE_PATH_%_1%_%PLATFORM%=%ProgramFiles%\VSC\bin"
set "EXE_PATH_NAME=EXE_PATH_%%_1%%_%%PLATFORM%%"
set "EXE_PATH_NAME=%EXE_PATH_NAME%"
set EXE_PATH_
endlocal

This batch code outputs for example:

EXE_PATH_NAME=EXE_PATH_%_1%_%PLATFORM%
EXE_PATH_VSC_x64=C:\Program Files\VSC\bin

It is necessary to use CALL to expand the variables in value of EXE_PATH_NAME, i.e. use this batch code with one more line according to comment by JosefZ which sets variable EXE_PATH_VALUE:

@echo off
setlocal
set "_1=VSC"
set "PLATFORM=x64"
set "EXE_PATH_%_1%_%PLATFORM%=%ProgramFiles%\VSC\bin"
set "EXE_PATH_NAME=EXE_PATH_%%_1%%_%%PLATFORM%%"
call set "EXE_PATH_NAME=%EXE_PATH_NAME%"
call set "EXE_PATH_VALUE=%%EXE_PATH_%_1%_%PLATFORM%%%"
set EXE_PATH_
endlocal

This batch code with additional call outputs for example:

EXE_PATH_NAME=EXE_PATH_VSC_x64
EXE_PATH_VALUE=C:\Program Files\VSC\bin
EXE_PATH_VSC_x64=C:\Program Files\VSC\bin

But I really don't understand why making this so complicated.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143