![]() |
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-09-23 13:55 |
47,812 topics / 349,917 posts / today 0 new / 48,273 members |
| DOS批处理 & 脚本技术(批处理室) » Question: How do you write "press any key to exit" in a batch file? |
| Printable Version 4,554 / 10 |
| Floor1 xiaojun | Posted 2003-12-26 00:00 |
| 银牌会员 Posts 499 Credits 2,202 | |
|
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...!
|
|
| Floor2 yoyobye | Posted 2003-12-27 00:00 |
| 初级用户 Posts 1 Credits 105 | |
|
pause will display “Press any key to exit” on the screen
|
|
| Floor3 shoushi | Posted 2003-12-27 00:00 |
| 初级用户 Posts 4 Credits 119 | |
| Floor4 xiaojun | Posted 2003-12-28 00:00 |
| 银牌会员 Posts 499 Credits 2,202 | |
|
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...
|
|
| Floor5 willsort | Posted 2003-12-30 00:00 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
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 ![]() |
|
| Floor6 xiaojun | Posted 2003-12-30 00:00 |
| 银牌会员 Posts 499 Credits 2,202 | |
|
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...!
|
|
| Floor7 willsort | Posted 2003-12-31 00:00 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
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. |
|
| Floor8 xiaojun | Posted 2003-12-31 00:00 |
| 银牌会员 Posts 499 Credits 2,202 | |
|
Hehe... many thanks for the guidance from the poster above
![]() |
|
| Floor9 hunome | Posted 2004-01-02 00:00 |
| 银牌会员 Posts 721 Credits 2,265 | |
|
Listening to willsort's words is better than reading books for months!
|
|
| Floor10 tdj | Posted 2004-01-02 00:00 |
| 银牌会员 Posts 332 Credits 1,131 | |
|
Great post!!! Collected.
|
|
| Floor11 xiaojun | Posted 2004-01-02 00:00 |
| 银牌会员 Posts 499 Credits 2,202 | |
|
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...
|
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |