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-07-02 13:21
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » GOTO:EOF Newcomers who don't know how to use it, please look here View 4,987 Replies 11
Original Poster Posted 2007-12-09 23:51 ·  中国 浙江 台州 电信
中级用户
★★
Credits 224
Posts 102
Joined 2007-11-09 00:19
18-year member
UID 102111
Gender Male
Status Offline
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.
Floor 2 Posted 2007-12-09 23:58 ·  中国 上海 松江区 电信
铂金会员
★★★★
DOS一根葱
Credits 5,493
Posts 2,315
Joined 2006-05-01 10:41
20-year member
UID 54766
Gender Male
From 上海
Status Offline
Floor 3 Posted 2007-12-10 04:10 ·  中国 浙江 台州 电信
中级用户
★★
Credits 224
Posts 102
Joined 2007-11-09 00:19
18-year member
UID 102111
Gender Male
Status Offline
fastslz
What are you doing, brother? I'm posting for newbies like me. There's no clearer post than this. Hehe
Floor 4 Posted 2008-03-05 07:53 ·  中国 黑龙江 鸡西 虎林市 联通
初级用户
Credits 25
Posts 14
Joined 2006-11-02 01:05
19-year member
UID 69226
Gender Male
Status Offline
Thanks to the landlord, this is a very good post for me to understand goto :eof
Floor 5 Posted 2008-07-12 13:28 ·  中国 宁夏 石嘴山 电信
新手上路
Credits 11
Posts 6
Joined 2008-07-12 12:57
17-year member
UID 121402
Gender Male
Status Offline
It seems that the result is the same whether there is goto :eof or not?
Floor 6 Posted 2008-07-14 13:32 ·  中国 北京 海淀区 联通
初级用户
Credits 22
Posts 15
Joined 2008-06-23 05:01
18-year member
UID 120728
Gender Male
Status Offline
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...
一起蹲坑
Floor 7 Posted 2008-07-14 14:53 ·  美国 惠普HP
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
All jumps should preferably use colons, as discussed before.
Floor 8 Posted 2008-07-24 15:37 ·  中国 广东 广州 电信
中级用户
★★
Credits 233
Posts 117
Joined 2007-11-28 02:38
18-year member
UID 104005
Gender Male
Status Offline
Originally posted by jdshaw at 2008-7-12 01:28 PM:
It seems that the result is the same with or without goto :eof?


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,
Floor 9 Posted 2008-07-27 01:55 ·  中国 广东 深圳 电信
新手上路
Credits 18
Posts 7
Joined 2008-07-24 10:43
17-year member
UID 121991
Gender Male
Status Offline
Can't understand, jumping around. Got dizzy.. Hope the LZ can speak simpler, take care of us newbies
Floor 10 Posted 2011-01-06 11:22 ·  中国 河北 保定 教育网
新手上路
Credits 11
Posts 6
Joined 2011-01-01 14:54
15-year member
UID 180183
Gender Male
From 河北石家庄
Status Offline
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.
Floor 11 Posted 2011-01-06 13:02 ·  中国 江苏 泰州 电信
新手上路
Credits 1
Posts 1
Joined 2011-01-05 21:55
15-year member
UID 180382
Gender Female
Status Offline
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.
Floor 12 Posted 2011-01-14 22:36 ·  中国 广东 佛山 顺德区 电信
初级用户
★★
Credits 151
Posts 106
Joined 2009-10-09 21:24
16-year member
UID 152856
Gender Male
From 河南省
Status Offline
It's this one that taught me GOTO :EOF
Forum Jump: