本文来自死性不改的网志
http://www.clxp.net.cn 转载请保留此申明!
关于同一批处理使用多个同样的命令,把这个用了多次的批处理改为一个子程序,用 call调用。
具体写法是
goto :eof
:一个标记
批处理内容
goto :eof
调用子程序的方法是
call :一个标记
另外要注意,子程序要放到脚本的最后,如果有多个子程序用:标记来划分就可以了。
如果是同一个功能的命令的话,可以放到一个子程序里,如果是不同功能的命令的话要放两个子程序
例1:
taskkill /f /im 1.exe
taskkill /f /im 2.exe
taskkill /f /im 3.exe
此例子视为同一个功能的命令,那么就写
goto :eof
:结束程序(这是注释哈,自己随便定义的,这就是那个标记)
taskkill /f /im 1.exe
taskkill /f /im 2.exe
taskkill /f /im 3.exe
goto :eof
这就是一段同一个功能的命令并连续执行的子程序。。。
在使用的时候直接用 call :结束程序 来调用此段子程序。
那么另一段功能不同,而且也是一个同一批处理使用多次的命令要组成子程序就要用goto :eof分隔开,
并用:标记,来区分。
例2:
下面这段代码我用了多次
taskkill /f /im 1.exe
taskkill /f /im 2.exe
taskkill /f /im 3.exe
这段代码也用了多次
md c:\1.txt
md c:\2.txt
md c:\3.txt
但是并不是结束完程序,就开始建立文件,中间还有其他语句,那么把他们改为子程序来调用如何写呢?看下面
goto :eof
:结束程序
taskkill /f /im 1.exe
taskkill /f /im 2.exe
taskkill /f /im 3.exe
goto :eof(不同功能的子程序要用goto :eof来分隔一下,用:标记来区分。)
:建立文件
md c:\1.txt
md c:\2.txt
md c:\3.txt
goto :eof
调用子程序的时候用 call :标记 来使用,
比如
call :结束程序
call :建立文件
为什么两个都是常用的命令,却要用goto :eof 来分隔开呢。。
当然是有原因的了。。因为这两段程序“结束程序”和“建立文件”两个子程序,是在同一个批处理里用了好多次。
但是他们并不是同时使用的,也就是说,并不是结束完程序,就建立文件,而是结束完程序,还有其他内容。然后
又用到建立文件,所以要用goto :eof来分隔一下。
普通的跳转goto 执行跳转是直接 goto 标记
但是子程序调用是 goto :标记 标记前面多了个冒号。。所以要注意。。。
表达的不是很明白,有点晕哈~~
写个完整的例子看看吧。
对照例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
批处理的意思就是
ping完192.168.0.1 后,关闭程序1.exe 2.exe 3.exe 、
ping完192.168.0.2 后,关闭程序1.exe 2.exe 3.exe 、
ping完192.168.0.3 后,关闭程序1.exe 2.exe 3.exe 、
退出
那么“关闭程序1.exe 2.exe 3.exe”这段代码在同一程序里用了3次,写这么长,多麻烦。。那么我们给他加进
子程序里去....
ping 192.168.0.1
call :结束程序
ping 192.168.0.2
call :结束程序
ping 192.168.0.3
call :结束程序
exit
goto :eof
:结束程序
taskkill /f /im 1.exe
taskkill /f /im 2.exe
taskkill /f /im 3.exe
goto :eof
使用子程序,和不使用子程序的代码,看起来有没有规范些,清爽些呢?
这只是简单的脚本,如果是一个很麻烦的命令,一个脚本里用了N次,如果每次都写同样的代码,,会很烦的,所以
还是建议大家在同一个批处理使用过多次的命令或语句,就搞成子程序来调用好了。
刚才例2就有点麻烦了。。。写下看看。
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
批处理的意思就是
ping完192.168.0.1 后,关闭程序1.exe 2.exe 3.exe 、
ping完192.168.0.2 后,关闭程序1.exe 2.exe 3.exe 、
ping完192.168.0.3 后,关闭程序1.exe 2.exe 3.exe 、
ping完192.168.0.4 后,建立名为1 2 3的三个文本 、
ping完192.168.0.5 后,建立名为1 2 3的三个文本 、
ping完192.168.0.6 后,建立名为1 2 3的三个文本 、
退出
这个就看出麻烦来了吧。。。
那好,,把用了N次的命令写进子程序里
ping 192.168.0.1
call :结束程序
ping 192.168.0.2
call :结束程序
ping 192.168.0.3
call :结束程序
ping 192.168.0.4
call :建立文件
ping 192.168.0.5
call :建立文件
ping 192.168.0.6
call :建立文件
exit
goto :eof
:结束程序
taskkill /f /im 1.exe
taskkill /f /im 2.exe
taskkill /f /im 3.exe
goto :eof
:建立文件
md c:\1.txt
md c:\2.txt
md c:\3.txt
goto :eof
我都看懂了蛤 你们不会还不会吧 蛤蛤 终于明白了GOTO:EOF的用法 希望 和我一样迷糊的朋友学会蛤
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.