China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-09 07:35
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Why?] In a DO……LOOP loop, why does it loop back in reverse after finishing? View 1,251 Replies 7
Original Poster Posted 2007-04-26 11:53 ·  中国 广东 广州 鹏博士宽带
初级用户
★★
Credits 176
Posts 78
Joined 2007-04-15 10:53
19-year member
UID 85268
Gender Male
Status Offline
The code is as follows. Please ask an expert to help analyze it... eagerly waiting


  1. @echo off
  2. setlocal enabledelayedexpansion
  3. color 0e
  4. mode con lines=20 cols=60
  5. set count=-1
  6. set a=Today is another healthy, happy, positive, and progressive day. Lift your head, oh hey, walk forward, oh hey... ——Breathe the New World
  7. :loop
  8. set /a count+=1
  9. call set temp=%%a:~!count!,1%%
  10. if "%temp%"=="" (
  11. goto out
  12. ) else (
  13. call set /p=%%a:~!count!,1%%<nul
  14. call :sound "%temp%"
  15. )
  16. :sound
  17. mshta vbscript:createobject("sapi.spvoice").speak(%1)(window.close)
  18. goto loop
  19. :out
  20. echo.
  21. pause>nul
Posted by Vampire: 2007-04-25 22:41


[ Last edited by Vampire on 2007-4-25 at 11:03 PM ]
Floor 2 Posted 2007-04-26 12:06 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
Try adding a goto :EOF below mshta.
Floor 3 Posted 2007-04-27 02:59 ·  中国 广东 广州 鹏博士宽带
初级用户
★★
Credits 176
Posts 78
Joined 2007-04-15 10:53
19-year member
UID 85268
Gender Male
Status Offline
Thanks for moderator lxmxn's reply. If I add goto :EOF below mshta, it gives an error; if I add it below :out, it still loops back.
Floor 4 Posted 2007-04-27 03:55 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
Floor 5 Posted 2007-04-27 07:35 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
21-year member
UID 44210
Status Offline
Floor 6 Posted 2007-04-29 05:20 ·  中国 广东 广州 鹏博士宽带
初级用户
★★
Credits 176
Posts 78
Joined 2007-04-15 10:53
19-year member
UID 85268
Gender Male
Status Offline
Thanks to the two posters above for the replies, I learned two more things. I've been a bit busy these past two days so I read it slowly, please forgive me. One more question: in the help file, goto :eof is explained like this:

If command extensions are enabled (the default), and you use a goto command with the target label :EOF, control can be transferred to the end of the current batch script file without defining the label, and then the batch script file is exited.

But then why can it still continue executing the loop? Could it be because it is only a called procedure, and has no authority to end the global environment?

[ Last edited by Vampire on 2007-4-28 at 04:27 PM ]
Floor 7 Posted 2007-09-13 17:37 ·  中国 北京 鹏博士BGP
初级用户
Credits 66
Posts 29
Joined 2007-09-06 15:09
18-year member
UID 96715
Gender Male
Status Offline
After analyzing it, I feel the OP's code is wrong at the goto back to loop in :sonud, because at that point the sonud subroutine has not ended yet. Only when temp finally becomes empty does it reach out, and at that time the last sound finishes, then it backs out one by one.

eof can only exit the current subroutine, right?

Not sure whether that's correct.

[ Last edited by flamemperor on 2007-9-13 at 05:58 PM ]
Floor 8 Posted 2008-07-21 19:14 ·  中国 广西 南宁 马山县 电信
初级用户
★★
Credits 176
Posts 78
Joined 2007-04-15 10:53
19-year member
UID 85268
Gender Male
Status Offline
The poster above got it exactly right...
Forum Jump: