![]() |
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 14:10 |
47,812 topics / 349,917 posts / today 0 new / 48,273 members |
| DOS批处理 & 脚本技术(批处理室) » GOTO:EOF Newcomers who don't know how to use it, please look here |
| Printable Version 5,512 / 11 |
| Floor1 YoDe | Posted 2007-12-09 23:51 |
| 中级用户 Posts 102 Credits 224 | |
|
This article is from the blog of Sixingbugai http://www.clxp.net.cn. Please retain this statement when reposting!
Regarding using multiple identical commands in the same batch processing, change this batch processing that is used multiple times into a subroutine and call it using call. The specific writing method is goto :eof :a mark Batch processing content goto :eof The method of calling the subroutine is call :a mark Also, note that the subroutine should be placed at the end of the script. If there are multiple subroutines, they can be divided by : marks. If it is the command of the same function, it can be put into one subroutine. If it is the command of different functions, two subroutines should be put. Example 1: taskkill /f /im 1.exe taskkill /f /im 2.exe taskkill /f /im 3.exe In this example, it is regarded as the command of the same function, then write goto :eof :End program (this is a comment, defined by yourself, this is that mark) taskkill /f /im 1.exe taskkill /f /im 2.exe taskkill /f /im 3.exe goto :eof This is a subroutine that continuously executes the commands of the same function... When using it, directly use call :End program to call this subroutine. Then another function is different, and the command that is also used multiple times in the same batch processing to form a subroutine should be separated by goto :eof, and use : marks to distinguish. Example 2: The following code I used many times taskkill /f /im 1.exe taskkill /f /im 2.exe taskkill /f /im 3.exe This code was also used many times md c:\1.txt md c:\2.txt md c:\3.txt But it is not to end the program and then start to create files, there are other statements in the middle, then how to change them into subroutines to call? Look below goto :eof :End program taskkill /f /im 1.exe taskkill /f /im 2.exe taskkill /f /im 3.exe goto :eof (The subroutines of different functions should be separated by goto :eof, and distinguished by : marks.) :Create file md c:\1.txt md c:\2.txt md c:\3.txt goto :eof When calling the subroutine, use call :mark to use it, for example call :End program call :Create file Why are both commonly used commands, but need to be separated by goto :eof. Of course, there is a reason. Because these two programs "End program" and "Create file" are used many times in the same batch processing. But they are not used at the same time, that is to say, it is not to end the program and then create a file, but to end the program and there are other contents. Then and then use to create a file, so it is necessary to separate by goto :eof. The ordinary jump goto executes the jump directly as goto mark But the subroutine call is goto :mark, there is an extra colon in front of the mark. So pay attention... The expression is not very clear, a bit confusing haha~~ Write a complete example to see. According to Example 1 ping 192.168.0.1 taskkill /f /im 1.exe taskkill /f /im 2.exe taskkill /f /im 3.exe ping 192.168.0.2 taskkill /f /im 1.exe taskkill /f /im 2.exe taskkill /f /im 3.exe ping 192.168.0.3 taskkill /f /im 1.exe taskkill /f /im 2.exe taskkill /f /im 3.exe exit The meaning of the batch processing is After pinging 192.168.0.1, close programs 1.exe 2.exe 3.exe, After pinging 192.168.0.2, close programs 1.exe 2.exe 3.exe, After pinging 192.168.0.3, close programs 1.exe 2.exe 3.exe, Exit Then the code "close programs 1.exe 2.exe 3.exe" is used 3 times in the same program. Writing it so long is so troublesome. Then we add it into the subroutine.... ping 192.168.0.1 call :End program ping 192.168.0.2 call :End program ping 192.168.0.3 call :End program exit goto :eof :End program taskkill /f /im 1.exe taskkill /f /im 2.exe taskkill /f /im 3.exe goto :eof Is the code using subroutines and not using subroutines more standardized and refreshing? This is just a simple script. If there is a very troublesome command, and it is used N times in a script, if you write the same code each time, it will be very troublesome. So It is still recommended that if the command or statement used multiple times in the same batch processing, it is better to make it into a subroutine to call. Just now Example 2 was a bit troublesome... Write it down. ping 192.168.0.1 taskkill /f /im 1.exe taskkill /f /im 2.exe taskkill /f /im 3.exe ping 192.168.0.2 taskkill /f /im 1.exe taskkill /f /im 2.exe taskkill /f /im 3.exe ping 192.168.0.3 taskkill /f /im 1.exe taskkill /f /im 2.exe taskkill /f /im 3.exe ping 192.168.0.4 md c:\1.txt md c:\2.txt md c:\3.txt ping 192.168.0.5 md c:\1.txt md c:\2.txt md c:\3.txt ping 192.168.0.6 md c:\1.txt md c:\2.txt md c:\3.txt exie The meaning of the batch processing is After pinging 192.168.0.1, close programs 1.exe 2.exe 3.exe, After pinging 192.168.0.2, close programs 1.exe 2.exe 3.exe, After pinging 192.168.0.3, close programs 1.exe 2.exe 3.exe, After pinging 192.168.0.4, create three text files named 1, 2, 3, After pinging 192.168.0.5, create three text files named 1, 2, 3, After pinging 192.168.0.6, create three text files named 1, 2, 3, Exit This is troublesome to see... Okay, write the commands used N times into subroutines ping 192.168.0.1 call :End program ping 192.168.0.2 call :End program ping 192.168.0.3 call :End program ping 192.168.0.4 call :Create file ping 192.168.0.5 call :Create file ping 192.168.0.6 call :Create file exit goto :eof :End program taskkill /f /im 1.exe taskkill /f /im 2.exe taskkill /f /im 3.exe goto :eof :Create file md c:\1.txt md c:\2.txt md c:\3.txt goto :eof I all understand. You won't still not understand, haha. Finally understand the usage of GOTO:EOF. I hope that friends who are as confused as me can learn it. |
|
| Floor2 fastslz | Posted 2007-12-09 23:58 |
| 铂金会员 Posts 2,315 Credits 5,493 From 上海 | |
| Floor3 YoDe | Posted 2007-12-10 04:10 |
| 中级用户 Posts 102 Credits 224 | |
|
fastslz
What are you doing, brother? I'm posting for newbies like me. There's no clearer post than this. Hehe |
|
| Floor4 windowsxphao | Posted 2008-03-05 07:53 |
| 初级用户 Posts 14 Credits 25 | |
|
Thanks to the landlord, this is a very good post for me to understand goto :eof
|
|
| Floor5 jdshaw | Posted 2008-07-12 13:28 |
| 新手上路 Posts 6 Credits 11 | |
|
It seems that the result is the same whether there is goto :eof or not?
|
|
| Floor6 sparklt | Posted 2008-07-14 13:32 |
| 初级用户 Posts 15 Credits 22 | |
|
Another thing I learned:
In normal jump, using goto, you directly use goto label. But for subroutine call, it's goto :label, with a colon before the label. So be careful... |
|
| Floor7 HAT | Posted 2008-07-14 14:53 |
| 版主 Posts 5,017 Credits 9,023 | |
|
All jumps should preferably use colons, as discussed before.
|
|
| Floor8 dslz666 | Posted 2008-07-24 15:37 |
| 中级用户 Posts 117 Credits 233 | |
Originally posted by jdshaw at 2008-7-12 01:28 PM: No way, the result is the same with or without goto :eof I suspect that this brother didn't read the article transferred by the landlord carefully, |
|
| Floor9 smyhcf | Posted 2008-07-27 01:55 |
| 新手上路 Posts 7 Credits 18 | |
|
Can't understand, jumping around. Got dizzy.. Hope the LZ can speak simpler, take care of us newbies
|
|
| Floor10 dos295730797 | Posted 2011-01-06 11:22 |
| 新手上路 Posts 6 Credits 11 From 河北石家庄 | |
|
Hehe, take it step by step, brother. If you don't understand something, look it up online. Only by settling down and studying can you learn it.
|
|
| Floor11 ylsmjh | Posted 2011-01-06 13:02 |
| 新手上路 Posts 1 Credits 1 | |
|
I didn't understand it very well. The function of goto:eof is that after calling a subroutine, it returns to the place where the Call was made.
|
|
| Floor12 zaixinxiangnian | Posted 2011-01-14 22:36 |
| 初级用户 Posts 106 Credits 151 From 河南省 | |
|
It's this one that taught me GOTO :EOF
|
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |