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.
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.


