开发者

How to apply %% to a variable in a script with input parameter?

开发者 https://www.devze.com 2022-12-31 18:19 出处:网络
I asked a question yesterday about how to manage to get %% around a variable without getting the evaluation.

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%

0

精彩评论

暂无评论...
验证码 换一张
取 消