批处理脚本如何将变量设置为另一个变量

Batch Script How to Set Variabes into another variable

我问了一个问题,但没有完全解释,所以这是我的问题。

我在里面有 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 文件中,我正在调用 Getdata.bat 文件

在我的主 .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

我测试了您发布的代码和数据,以及建议的解决方案。这些是我在测试中使用的文件。

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 ].