中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-01 19:02
48,037 topics / 350,122 posts / today 2 new / 48,249 members
DOS批处理 & 脚本技术(批处理室) » My auto-exit countdown
Printable Version  1,272 / 5
Floor1 austion Posted 2006-06-07 17:05
初级用户 Posts 82 Credits 178
I referred to some material online and made an auto-exit countdown. The code is as follows:
@echo off
set t=5
set a=1

:EX
cls
color 2F
echo ---------------------------------------------
echo.
echo auto exit after %t% seconds......
echo.
echo ---------------------------------------------
call :ProcDelay EX0
:EX0
set /a t=%t%-%a%
if %t% GTR 0 goto EX
@exit


:ProcDelay delayMSec_
setlocal enableextensions
for /f "tokens=1-4 delims=:. " %%h in ("%time%") do set start_=%%h%%i%%j%%k
:_procwaitloop
for /f "tokens=1-4 delims=:. " %%h in ("%time%") do set now_=%%h%%i%%j%%k
set /a diff_=%now_%-%start_%
if %diff_% LSS 100 goto _procwaitloop
endlocal & goto %1



It works very well, it exits after 5 seconds, and it can count down too.
But I want to ask whether this code can be simplified?
Can the :EX0 part be simplified away? Having to use goto here is very troublesome.

And also this part inside it:

setlocal enableextensions
..................
endlocal

What are these markers for? Can someone explain?
Floor2 bagpipe Posted 2006-06-07 17:39
银牌会员 Posts 425 Credits 1,144 From 北京
You made this yourself??? Hehe, I think you just pasted someone else's code. willsort's delay batch file happens to have this section of code. Hehe. Sigh, setlocal enableextensions enables command processor extensions, and endlocal ends its scope of use. If endlocal appears at the end of the batch file it doesn't mean very much, since by default it will execute the endlocal statement anyway.
Floor3 austion Posted 2006-06-07 17:47
初级用户 Posts 82 Credits 178
:ProcDelay delayMSec_
setlocal enableextensions
for /f "tokens=1-4 delims=:. " %%h in ("%time%") do set start_=%%h%%i%%j%%k
:_procwaitloop
for /f "tokens=1-4 delims=:. " %%h in ("%time%") do set now_=%%h%%i%%j%%k
set /a diff_=%now_%-%start_%
if %diff_% LSS 100 goto _procwaitloop
endlocal & goto %1

This is someone else's code with a few changes.

What I want to know is whether the code in front can be simplified. I especially don't want to use goto to display the countdown automatically.



After calling it in the program, does it return to the place where call was used (the parent batch file)?

[ Last edited by austion on 2006-6-7 at 18:47 ]
Floor4 fastslz Posted 2006-06-08 22:03
铂金会员 Posts 2,315 Credits 5,493 From 上海
There is a Wait delay program that's only 509 bytes, but after running it under the CMD command line it jumps into command mode.
Floor5 willsort Posted 2006-06-09 02:54
元老会员 Posts 1,512 Credits 4,432
Re austion:

The OP may not yet have grasped how to use call :label. Similar to call program.bat, after it calls a section of code in the batch file itself according to the label, it will automatically return to after the call statement, so you do not need to jump back with goto after :ProcDelay ends, and therefore you also do not need the ex0 label or the corresponding command-line parameter.

Re bagpipe:

Using endlocal at the end of a program is sometimes useful. For example, it can be used to preserve environment variable changes made between setlocal/endlocal. For details see .

Re fastslz:

It jumps into command mode because it is a 16-bit MZ format program; if a 32-bit PE program is used, this problem will not occur.

Herbert Kleebauer's sleep.bat is mentioned in . It can directly generate the sleep.exe program. Because this program has both a 16-bit MZ module and a 32-bit PE module, it is an "amphibious" program that can run both in DOS and in the Windows command line.

Its size is 1024 bytes, which is the minimum limit for the PE format.

Problem extracting file contents line by line and doing calculations! Please, experts, help answer.!
http://www.cn-dos.net/forum/viewthread.php?tid=21075#pid126083

An oddity in batch programming — Clock
http://www.cn-dos.net/forum/viewthread.php?tid=8905#pid54227
Floor6 austion Posted 2006-06-10 01:35
初级用户 Posts 82 Credits 178
Thanks to post #5 for the explanation, but according to what you said, then mine can be made like this:

@echo off
set t=5
set a=1

:EX
cls
color 2F
echo ---------------------------------------------
echo.
echo auto exit after %t% seconds......
echo.
echo ---------------------------------------------
call :ProcDelay
set /a t=%t%-%a%
if %t% GTR 0 goto EX
@exit


:ProcDelay
setlocal enableextensions
for /f "tokens=1-4 delims=:. " %%h in ("%time%") do set start_=%%h%%i%%j%%k
:_procwaitloop
for /f "tokens=1-4 delims=:. " %%h in ("%time%") do set now_=%%h%%i%%j%%k
set /a diff_=%now_%-%start_%
if %diff_% LSS 100 goto _procwaitloop
endlocal



In this code, it indeed runs without any problem, everything is normal.
But if this :ProcDelay code is not placed at the end, and there is still code after it, then the problem appears.
After it finishes executing :ProcDelay, instead of returning to the parent batch file, it executes the code below.
That really is a problem.
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023