设置未设置的变量,范围CMD脚本Windows,例程

时间:2014-11-21 01:09:05

标签: variables batch-file scope subroutine

我想知道如何解决这个问题:

rem declaring variable
set "vara="
echo vara:%vara% bef
call :myroutine
echo vara:%vara% aft

:routine
rem ...
rem assing value to variable vara
set "vara=34"
echo vara was assigned...
rem ....
echo vara:%vara%
exit /b

现在我有了这个:

vara: bef
vara was assigned
vara:34
vara: aft

我需要使用常规之后的值,就像这样......

vara: bef
vara was assigned
vara:34
vara:34 aft

vara: 34 船尾 怎么做?

1 个答案:

答案 0 :(得分:1)

这就是已经发生的事情,假设您拨打了正确的标签。 (:myroutine!=:例程)。您的脚本还存在其他一些小问题(在goto :EOF之前遗失:routine,以及@echo offsetlocal。这是固定的:

@echo off
setlocal
rem declaring variable
set "vara="
echo vara:%vara% bef
call :routine
echo vara:%vara% aft

set "vara="
goto :EOF

:routine
rem ...
rem assing value to variable vara
set "vara=34"
echo vara was assigned...
rem ....
echo vara:%vara%
exit /b

有关调用子例程/函数的更多信息,请参阅this page,特别注意有关返回值的不同方法的部分。您在问题中描述的内容已经按照您的要求运行,除非您setlocal:routine