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-01 10:17
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Question: How do you write "press any key to exit" in a batch file? View 4,398 Replies 10
Original Poster Posted 2003-12-26 00:00 ·  中国 重庆 电信
银牌会员
★★★
Credits 2,202
Posts 499
Joined 2003-06-12 00:00
23-year member
UID 4876
Gender Male
Status Offline
In setup 98 and when installing some programs, there is a press-any-key exit (or ESC key); it can exit directly or show a prompt. How can this function be implemented in a batch file? Thanks...!
一年四季,枫叶红了又红;人生四季,失去的,还能再来吗?—— !
Floor 2 Posted 2003-12-27 00:00 ·  中国 北京
初级用户
Credits 105
Posts 1
Joined 2003-12-27 00:00
22-year member
UID 14525
Gender Male
Status Offline
pause will display “Press any key to exit” on the screen
Floor 3 Posted 2003-12-27 00:00 ·  中国 黑龙江 哈尔滨 教育网
初级用户
Credits 119
Posts 4
Joined 2003-12-27 00:00
22-year member
UID 14539
Gender Male
Status Offline
Floor 4 Posted 2003-12-28 00:00 ·  中国 重庆 电信
银牌会员
★★★
Credits 2,202
Posts 499
Joined 2003-06-12 00:00
23-year member
UID 4876
Gender Male
Status Offline
Sorry, I didn't explain it clearly; just like when installing win98 or XP, during the process you can press ESC to exit; and in the installation of some DOS programs under DOS, you often see a prompt saying press any key to continue, but press ESC to exit. How can this press-any-key-to-continue, ESC-to-exit function be done in a batch file? Thanks for the help...
一年四季,枫叶红了又红;人生四季,失去的,还能再来吗?—— !
Floor 5 Posted 2003-12-30 00:00 ·  中国 山东 烟台 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re xiaojun:

pause works as press any key to continue, press Ctrl+C or Ctrl+Break to exit; when run in a batch file, it just so happens to also have the exit prompt you need.

echo Press any key to continue and ctrl+c key to exit
pause>nul

Maybe you still feel unsatisfied, wondering why there can't be a more user-friendly exit key setting and a more user-friendly exit prompt? Let me give you another suggestion:

choice /c:qc /n /t:c,5 Press 'q' key to exit and 'c' key to continue.
if errorlevel 2 goto continue
if errorlevel 1 goto exit

:exit
echo.
choice Are you exit really?
if errorlevel 2 goto continue
goto end

Of course, there are still many limitations: you can only use letters and number keys for control, and can't use Esc, space, Enter, and so on. That's the limitation of batch files themselves. If you want to use them, you have to accept that limitation, or work around it, or break through it
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 6 Posted 2003-12-30 00:00 ·  中国 重庆 电信
银牌会员
★★★
Credits 2,202
Posts 499
Joined 2003-06-12 00:00
23-year member
UID 4876
Gender Male
Status Offline
Thanks for the help from the poster above. I still didn't explain it clearly. What the above does is delay for a few seconds and give 2 choices before continuing with the next part, but I think I understand one thing now: maybe batch files can't do “in the installation of some DOS programs, press any key to continue, but ESC exits” — but IF you don't press anything, the installation keeps running. For example, during XP setup, there is esc exit in the lower left corner, and after pressing ESC, a confirmation dialog appears. This probably isn't something batch files can do, right? It involves interrupts and programming, right? Is my understanding of this question correct? Thank you...!
一年四季,枫叶红了又红;人生四季,失去的,还能再来吗?—— !
Floor 7 Posted 2003-12-31 00:00 ·  中国 山东 烟台 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re xiaojun:

After reading your reply, I feel there are a few points that need to be clarified:

1. Under dos, there are very few things batch files can't do—almost none! At least up to now, I have never run into a dos case that definitely could not be implemented with batch files. Why do I say this? Because the greatest strength of batch files is that they can call on every resource that can be called, including all kinds of internal and external commands, all kinds of programs, tools, and software that can be found, and even source code in various languages can be directly interpreted and executed in batch files, asm, bas, c, and the like. By organically combining all these resources, the functions and interfaces that can be achieved are beyond what anyone can imagine. So you could say batch files fully embody the bold saying, “Nothing is impossible, only things you haven't thought of.”

2. Under dos, the occasions where batch files are truly used are less than one tenth; many cases that could originally be easily implemented with batch files are abandoned by users. Why is that? Because of the cost issue—in other words, the effort put in and the result obtained cannot be brought into balance. There are many kinds of cost here: compatibility, generality, robustness, ease of use, applicability, and two other quite important ones, namely the space and time required to execute a batch program. Of course there are also many kinds of gains: a program that can run normally, a program that can run perfectly, a program that can run anywhere, the sense of achievement from programming it yourself, the satisfaction of having a complex problem solved simply, and so on. But I believe very few people write batch programs longer than 10 lines; I myself also rarely write programs over a hundred lines, mainly because of “complexity.” Complexity severely damages the lightness and speed of the program, and at the same time harms the various kinds of cost I mentioned. As for whether these costs can be traded for a higher return, that is what we need to consider in real programming. So in many cases, “it's not that it can't be done, it's that people won't do it.”

3. As for the problem you mentioned in the first post, it really isn't a very complicated case, but if you insist on implementing it through batch files, then it really does require some thought. But do you want the program you write, just for the sake of “pausing for two seconds,” to need to load a program that takes two minutes to initialize? That's where the crux of the problem lies.
But why do we still use batch files? First of all, of course, because of their simplicity—most people can write a practically usable program without much thought at all—which really is a great temptation. Another point, which probably quite a few people can also appreciate, is their trickiness—when you run into a problem and think “this can't be done,” only to be told that it can be easily “KO'd” with just two or three lines of code—that kind of pleasure is something many people are happy to experience. Although this kind of trickiness exists in other languages too, none of them make it feel as strong as batch files do. With only a bit more than 10 dedicated keywords available, yet being able to mobilize almost all resources, this contrast is rare in most languages. So even now, I, along with many others, still love batch files, although they are gradually growing old.

Finally, let me apologize to you—after all that, I still didn't completely solve your problem. It's not that I can't think of a way, it's that I don't want to think too much about it It seems human laziness really is inborn! I sincerely hope you can find the best way to solve the problem; maybe it lies in batch files.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 8 Posted 2003-12-31 00:00 ·  中国 重庆 电信
银牌会员
★★★
Credits 2,202
Posts 499
Joined 2003-06-12 00:00
23-year member
UID 4876
Gender Male
Status Offline
Hehe... many thanks for the guidance from the poster above
一年四季,枫叶红了又红;人生四季,失去的,还能再来吗?—— !
Floor 9 Posted 2004-01-02 00:00 ·  中国 北京 科技网
银牌会员
★★★
颓废青年
Credits 2,265
Posts 721
Joined 2003-05-12 00:00
23-year member
UID 2032
Gender Male
Status Offline
Listening to willsort's words is better than reading books for months!
Floor 10 Posted 2004-01-02 00:00 ·  中国 辽宁 抚顺 联通
银牌会员
★★★
论坛候鸟
Credits 1,131
Posts 332
Joined 2003-11-27 00:00
22-year member
UID 13461
Gender Male
Status Offline
Great post!!! Collected.
Floor 11 Posted 2004-01-02 00:00 ·  中国 重庆 电信
银牌会员
★★★
Credits 2,202
Posts 499
Joined 2003-06-12 00:00
23-year member
UID 4876
Gender Male
Status Offline
Batch files have gotten me totally confused and mixed up, and only now have I figured out what my question really is: ~If the key value of “ESC” is AAA, and the key value of “ENTER” is BBB; then in a batch file, after prompting ESC to exit and ENTER to continue; determine whether the user pressed AAA or BBB, and based on that decide whether to end the batch file or continue executing. I just can't write that crucial judgment line, hehe ~ ^_^ Many thanks for the help, getting close to the answer...


一年四季,枫叶红了又红;人生四季,失去的,还能再来吗?—— !
Forum Jump: