How to apply %% to a variable in a script with input parameter?
I asked a question yesterday about how to manage to get %% around a variable without getting the evaluation. Well, the thing is, it does not work that way in my case...
I have a .bat file which gets an input par开发者_开发问答ameter. Later on I want to use the value of this input parameter and put %...% around, like:
call script.bat testValue
script.bat:
set inputPar=%1
set newValue=%%inputPar%%
Now I get:
echo %inputPar%
testValue
echo %newValue%
%inputPar%
But I would like to get:
echo %newValue%
%testValue%
Is that somehow possible?
The reason you're getting those results is because %%inputPar%%
is being treated as %%inputPar%% and %%
becomes a literal %
.
You need:
first.cmd:
@echo off
call script.cmd testValue
script.cmd:
@echo off
set inputPar=%1
set newValue=%%%inputPar%%%
echo %inputPar% should be testValue
echo %newValue% should be %%testValue%%
This gives you %%%inputPar%%% and, again, %%
becomes a literal %
with %inputPar%
becoming testValue
.
I found the solution:
set inputPar=%1
set inputParDollar=%%%inputPar%%%
:: installDir is an already set environment variable
set installDirDollar=%%%installDir%%%
echo inputPar: %inputPar%
echo inputParDollar: %inputParDollar%
echo installDirDollar: %installDirDollar%
call script.bat testValue
inputPar: testValue
inputParDollar: %testValue%
installDirDollar: %D:\testenv%
精彩评论