Windows批处理功能被称为额外的时间

问题描述 投票:1回答:2

我有一个批处理脚本编译和运行Java程序,并因为它是这样它打印任务时开始的时间戳。我注意到,:printDate函数被调用在最后一个额外的时间,但之后:exit应该结束脚本“完成......”被打印出来。

@echo off

set PRGM=Foo
cls

call :printDate
echo Compiling...
javac %PRGM%.java

call :printDate
echo Executing...
java %PRGM%

call :printDate
echo Results...
type output.txt

call :exit

:: ----------------------------------------------------------
:: Functions
:: ----------------------------------------------------------

:printDate
for /f "tokens=2-4 delims=/ " %%a in ('echo %DATE%') do (set mydate=%%c/%%a/%%b)
for /f "tokens=1-3 delims=/:./ " %%a in ('echo %TIME%') do (set mytime=%%a:%%b:%%c)
echo|set /p=[%mydate% %mytime%] 
goto:eof

:exit
call:printDate
echo Done...
goto:eof

这里是我的输出

[2013/10/17 21:26:11] Compiling...
[2013/10/17 21:26:12] Executing...
[2013/10/17 21:26:12] Results...
2
6
6
5
[2013/10/17 21:26:12] Done...
[2013/10/17 21:26:12]

编辑

如果有人有兴趣,这是我工作的脚本:http://pastebin.com/xfwStvNK。我的Java程序生成的输出文件和脚本生成的输入,编译和运行该程序后打印输出。

windows function batch-file javac goto
2个回答
1
投票

:printDate过程被称为另一个时间,因为调用:exit过程中,您呼叫的:printDate过程内,但:exit之后要返回到echo Done行,以便call :exit块处理一次,则:printDate当你不完成执行真的goto:eof:printDate线是脚本的真正结束。

这是一个Call的meanning,相反,你需要使用GoTo关键字,就像这样:

...
REM call :exit
Goto :Exit
...

...
:exit
call:printDate
echo Done...
REM goto:eof
Exit

0
投票

始终使用转到标签来终止你的主程序。

例:

echo hello world<br>
call:doFirstThing<br>
call:doSecondThing<br>
<p>
Goto :FinalExit<br>
<p>
:doFirstThing<br>
echo in the first method<br>
goto:eof<br>
<p>
:doSecondThing<br>
echo in the second method<br>
goto:eof<br>
<p>
FinalExit<br>
© www.soinside.com 2019 - 2024. All rights reserved.