结束":| _ |"标记,多个"转到" |函数创建(cmd | .bat)

时间:2018-06-12 13:55:20

标签: batch-file for-loop cmd

如何制作,可访问快捷方式(" goto | anyth |" |" call | anyth |" | ..)来自" for"循环,脚本,在1个脚本文件中

我的尝试:

for /F %%a in ("t1") do (
    if "%%a"=="" (
            call :fsc
    ) else (
        echo "n t"
        if "%%a"=="t1" (
            call :fsc
        ) else (
            call :fsc1
        )
    )
    :fsc
    echo t1
    rem how? to end the :fsc (without the addition it continuing to "pause")
    rem  (answer: not possible in bash, this example's way can't be used because "goto" breaks out ?all scopes)
    :fsc
    echo t2
)

pause

和其他一个

for /F %%a in ("t1") do (
    if "%%a"=="" (
            call :fsc
    ) else (
        echo "n t"
        if "%%a"=="t1" (
            call :fsc
        ) else (
            call :fsc1
        )
    )
)
:fsc
echo t1
goto :eof
:fsc1
echo t2
goto :eof

::why? after the "for" ends, execution doesn't coming here
:: (answer: all after "for" executed( including "goto :eof"))
pause

mintextlimitmintextlimitmintextlimitmintextlimitm

1 个答案:

答案 0 :(得分:0)

最佳做法是将您调用的函数放在脚本的末尾。这与大多数人在其他脚本语言中所做的相反。函数是其他脚本语言中的函数,但它们不能在批处理文件中以这种方式工作。因此,在批处理文件中,最佳做法是将它们放在脚本的末尾。

@ECHO OFF
for /F %%a in ("t1") do (
    if "%%a"=="" (
            call :fsc
    ) else (
        echo "n t"
        if "%%a"=="t1" (
            call :fsc
        ) else (
            call :fsc1
        )
    )
)
REM Put the main programming code here.
pause

REM END OF SCRIPT. ONLY FUNCTIONS BELOW.
GOTO :EOF
:fsc
echo t1
goto :eof

:fsc1
echo t2
goto :eof