2010-05-21 56 views
0

昨天我問了一個問題,關於如何在未得到評估的情況下獲得變量的%%。 嗯,事情是,它不工作在我的情況下...如何將%%應用於具有輸入參數的腳本中的變量?

我有一個.bat文件,它獲取輸入參數。後來我想用這個輸入參數的值,並把%...%左右,如:

call script.bat testValue 

script.bat:

set inputPar=%1 
set newValue=%%inputPar%% 

現在,我得到:

echo %inputPar% 
testValue 
echo %newValue% 
%inputPar% 

但我想獲得:

echo %newValue% 
%testValue% 

是不知何故可能?

回答

0

我發現瞭解決方案:

組inputPar =%1

組inputParDollar = %%% inputPar %%%

:: installDir是已經設定的環境變量

集installDirDollar = %%% installDir %%%

echo inputPar:%inputPar%

回聲inputParDollar:%inputParDollar%

回聲installDirDollar:%installDirDollar%


呼叫script.bat測試值

inputPar:測試值

inputParDollar:%測試值%

installDirDollar:%D:\ testenv%

2

你得到這些結果的原因是因爲%%inputPar%%被視爲%%ňpü噸P一個[R%%%%成爲文字%

您需要:

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

相關問題