Batch file 如何将%%应用于带有输入参数的脚本中的变量?
我昨天问了一个问题,关于如何在没有得到评估的情况下设法绕过一个变量。 嗯,问题是,我的情况不是这样的 我有一个.bat文件,它获取一个输入参数。稍后,我想使用此输入参数的值并将%…%放在周围,如:Batch file 如何将%%应用于带有输入参数的脚本中的变量?,batch-file,dos,Batch File,Dos,我昨天问了一个问题,关于如何在没有得到评估的情况下设法绕过一个变量。 嗯,问题是,我的情况不是这样的 我有一个.bat文件,它获取一个输入参数。稍后,我想使用此输入参数的值并将%…%放在周围,如: call script.bat testValue script.bat: set inputPar=%1 set newValue=%%inputPar%% 现在我得到: echo %inputPar% testValue echo %newValue% %inputPar% 但我想得
call script.bat testValue
script.bat:
set inputPar=%1
set newValue=%%inputPar%%
现在我得到:
echo %inputPar%
testValue
echo %newValue%
%inputPar%
但我想得到:
echo %newValue%
%testValue%
这有可能吗?我找到了解决方案:
设置inputPar=%1
设置inputParDollar=%%%inputPar%%
::installDir是一个已设置的环境变量
设置installDirDollar=%%%installDir%%
回显输入PAR:%inputPar%
回显inputpollar:%inputpollar%
echo installDirDollar:%installDirDollar%
调用script.bat testValue inputPar:testValue inputParDollar:%testValue%
installDirDollar:%D:\testenv%您之所以得到这些结果,是因为
%%inputPar%%
被视为%%inputPar%%,而%%
变成了文本%%
你需要:
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%%
这将为您提供%%%inputPar%%%,并且,%%
再次成为文本%%
,而%inputPar%%
将成为testValue