【问题标题】:Call ReadINI script using same one bat file使用同一个 bat 文件调用 ReadINI 脚本
【发布时间】:2011-12-28 14:38:34
【问题描述】:

我只需要使用一个 bat 文件,所以我将脚本复制到我的 bat 文件中。 如何调用此脚本,并在局部变量中使用 currval 值?

@setlocal enableextensions enabledelayedexpansion
@echo off
set file=%1
set area=[%2]
set key=%3
set currarea=
for /f "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

【问题讨论】:

    标签: batch-file ini


    【解决方案1】:

    我想你正在搜索函数。

    您可以将其粘贴到批处理文件中并通过call :myFunction 调用它,但由于批处理函数没有返回值,您需要通过将结果分配给变量来显式执行此操作。

    在您的情况下,您的代码可能如下所示

    @echo off
    setlocal enableextensions enabledelayedexpansion
    call :myFunction %1 %2 %3
    echo !result!
    exit /b
    
    :myFunction
    set file=%1
    set area=[%2]
    set key=%3
    set currarea=
    for /f "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!" (
                    set result=!currval!
                )
            )
        )
    )
    exit /b
    

    也许这并不完美,因为它只需要一个结果,但我希望它指向正确的方向。

    【讨论】:

      猜你喜欢
      • 2020-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-01
      相关资源
      最近更新 更多