2016-04-30 139 views
0

我問了一個問題,但它沒有完全解釋,所以這是我的問題。批處理腳本如何將變量設置爲另一個變量

我得到的Settings.ini裏面有一個與變量部分

[HideAndSeekEvent] 
EventRunning=[Hide And Seek Event] Has started at [ %placenameHS% ]. 

,得到了所謂的ini.bat

@setlocal enableextensions enabledelayedexpansion 
@echo off 
set file=%~1 
set area=[%~2] 
set key=%~3 
set currarea= 
for /f "usebackq delims=" %%a in ("!file!") do (
    set ln=%%a 
    if "x!ln:~0,1!"=="x[" (
     set currarea=!ln! 
    ) else (
     for /f "tokens=1,2 delims==" %%b in ("!ln!") do (
      set currkey=%%b 
      set currval=%%c 
      if "x!area!"=="x!currarea!" if "x!key!"=="x!currkey!" (
       echo !currval! 
      ) 
     ) 
    ) 
) 
endlocal 

另一個批處理文件,並呼籲Getdata.bat另一個bat文件調用變量從.ini文件和本節有

for /f "delims=" %%a in ('call ini.cmd settings.ini HideAndSeekEvent EventRunning') do (
    set EventRunningeHS=%%a 
) 

在我的主要.bat文件iam ca灌裝的Getdata.bat文件

在我的主蝙蝠我得到了這樣的

SET placenameHS=Hotan 
echo %EventRunningeHS% 

一些事情它應該顯示

[Hide And Seek Event] Has started at [ Hotan ]. 

但它顯示:

[Hide And Seek Event] Has started at [ %placenameHS% ]. 

[公告]我不設置我通過SQLCMD得到的地點名稱查詢

osql -S %Server% -U %SQLUser% -P %SQLPass% -d Dother_Events -Q "SET NOCOUNT ON; SELECT Place_Name FROM _HideANDSeek_Map WHERE ID = %IDHS%" %osqluser% -b -w 9999 -h-1 -o .\Logs_in_use\HSLN7.txt 
set /p HSLN7=<.\Logs_in_use\HSLN7.txt 
+0

'叫回聲%EventRunningeHS%'應該擴大內'%placenameHS%' – JosefZ

+2

@JosefZ:是,我已經在[我的回答](http://stackoverflow.com/questions/36958759/batch-script-how-to-set-variabes-inside-another-variable/36959503#36959503)中給出了其他OP的解決方案關於同一主題的問題... – Aacini

+0

@Aacini我預言這種問題必須在SO的某個地方有答案。懶得搜索:)但是,我覺得OP的問題已經詳盡闡述,並不值得通過標記爲重複來懲罰。請給你的答案... – JosefZ

回答

0

我測試了您發佈的代碼和數據以及建議的解決方案。這些是我在測試中使用的文件。

的Settings.ini

[HideAndSeekEvent] 
EventRunning=[Hide And Seek Event] Has started at [ %placenameHS% ]. 

ini.cmd

@setlocal enableextensions enabledelayedexpansion 
@echo off 
set file=%~1 
set area=[%~2] 
set key=%~3 
set currarea= 
for /f "usebackq delims=" %%a in ("!file!") do (
    set ln=%%a 
    if "x!ln:~0,1!"=="x[" (
     set currarea=!ln! 
    ) else (
     for /f "tokens=1,2 delims==" %%b in ("!ln!") do (
      set currkey=%%b 
      set currval=%%c 
      if "x!area!"=="x!currarea!" if "x!key!"=="x!currkey!" (
       echo !currval! 
      ) 
     ) 
    ) 
) 
endlocal 

Getdata.bat

@echo off 

for /f "delims=" %%a in ('call ini.cmd settings.ini HideAndSeekEvent EventRunning') do (
    set EventRunningeHS=%%a 
) 

main.bat

@echo off 

call Getdata.bat 

SET placenameHS=Hotan 

echo Original output: 
echo %EventRunningeHS% 

echo Proposed solution: 
call echo %EventRunningeHS% 

這是輸出當我運行main.bat:

Original output: 
[Hide And Seek Event] Has started at [ %placenameHS% ]. 
Proposed solution: 
[Hide And Seek Event] Has started at [ Hotan ]. 
相關問題