5.: and goto
Why do we need to jointly introduce these two commands? Because they are inseparable, no matter which one is missing or which one is more, they will make mistakes. goto is a jump command,: is a tag. When the program runs to goto, it will automatically jump to the: defined part to be executed (Is it inseparable?). In Example 5, a first line of the fifth last line appears:, and the program will automatically jump to the part of the label definition when it runs to goto, and the result is that the script usage (usage is the label name). It is not difficult to see that the goto command is to find the place it should jump based on the colon and the label name, and they are one-to-one corresponding relationships. The goto command is also often used in combination with the if command. As for the specific usage of these two commands, refer to Example 5.
Another usage of the goto command is one: end the program early. Use the goto command to jump to a certain tag in the middle of the program, and the content of this tag is defined as exit. like:
……
goto end
……
:end
Here: end is on the last line of the script! In fact, this example is very weak. You will know if command and combination command later.
6、%
Strictly speaking, this percentage sign is not a command. It is just a parameter in batch processing (except when multiple % are used together, which will be introduced in detail later), but don’t underestimate it if it is just a parameter (see how many places are used in Example 5?), and the batch processing function is reduced by 51%. Take a look at Example 7:
net use \\%1\ipc$ %3 /u:"%2" copy \\%1\admin$\system32 /y copy \\%1\admin$\system32 /y copy \\%1\admin$\system32 /y copy \\%1\admin$\system32 /y attrib \\%1\admin$\system32\ -r -h -s
The above code is part of the virus, %1 represents IP, 2% represents username, and 3% represents password. The execution form is: script file name Parameter 1 Parameter 2... Assuming that this script is saved as, the execution form is as follows: a IP username password. Here, IP, username, and password are three parameters, and none of them are missing (because the program cannot run correctly, not because the syntax is not correct if the parameter syntax is missing). In this way, during the script execution process, the script will automatically use your three parameters in sequence (remember, it is in sequence! It is also a one-to-one correspondence.) to replace 1%, 2% and 3%, so as to achieve the purpose of flexible use (imagine, if IP, username and password are directly defined in the script, then the function of the script will be fixed, but if % is used, different parameters can achieve different purposes. Isn’t it more flexible?).
The use of this parameter will be introduced in subsequent chapters. You must be very proficient, which requires a lot of practice and some hard work!
That's all for this chapter. Some friends may have asked: Why didn’t the if command be introduced? Haha, it’s not that I forgot, but that it’s not easy to explain clearly, I’ll talk about it in the next chapter! If you are a beginner, you may be able to digest the things you talk about in this chapter. Remember one sentence: DOS is a batch BODY, and any DOS command can be used in batch scripts to complete specific functions. At this point, have you thought of using the things in your belly to write something with automation? It's very simple, it's just a collection of DOS commands. I believe that you, who call yourself a genius, will have already completed the DOS part of the computer-level test questions in batches.
bother! It’s like a half-old woman who reaches menopause wants to nag about everything, feel uncomfortable in everything, and feel unhappy in anyone. Knowing that there is a tiger in the mountain, I tend to walk towards the tiger mountain. When I finally left a scar on my body and returned without help, I realized that I was so fragile, so small, and so vulnerable. On the verge of collapse, I suddenly recalled the moment when I beat someone for the last time I beheaded. I really missed him (actually, I don’t like beating someone very much, let alone being beaten). I needed to vent, and I typing on the keyboard with my fingers. In a series of rhythmic sounds, the above words appeared on the screen. But is this another way to vent? Chinese people are still powerful. As early as thousands of years ago, Confucius said, "Only women and villains are difficult to raise." He is really foresighted and admired! Although I am venting my temper, please rest assured that since I decided to write this tutorial, I will do my best to write it well and write it perfectly, and never leave any regrets for myself. Otherwise, I would not have written this tutorial!
There was once a classic batch processing tutorial that appeared on your screen. You did not save it. You would not regret it until you couldn't find its link. This is the greatest pain in the world. If God can give you a chance to read it again, you will say three words to that tutorial: I love you! If you have to add a deadline to this love, you hope it will be 100 years. Because 100 years later, you may have died! Now, this batch tutorial you are reading appears on your screen. Although it is not as good as the classic you have read, it can be passed if you can barely. Will you love it? Will it take 50 years to go? The answer is: give it a try.
The most important commands in batch scripts will be introduced in detail in this chapter, but unfortunately, I have not mastered some details very well until now, and I even have some real differences. Just like not knowing how to love very much. But I have been working hard, even if I have never gained anything. So what may be said will be more general, but I will tell you the method. The rest is a matter of time and you need to practice it yourself. Let's work together. It is not a day to freeze three feet, and it is not a day to freeze. Some things, such as learning batch processing, and loving someone, cannot be achieved quickly, and there will be even situations where hard work is done but the gains are small. Again, when reading this tutorial, you must calm down unless you have mastered everything about this tutorial--but there is no need to read it, a waste of time!
7、if
Continue with the previous chapter, and then talk about the if command. In general, the if command is a command that represents judgment. According to each result, it can correspond to a corresponding operation. The three usages of it are discussed here separately.
(1) Enter to judge. Let’s take a few sentences from use case 5:
if "%1"=="" goto usage if "%1"=="/?" goto usage if "%1"=="help" goto usage
Here we judge the input parameters. If the parameter is empty (no parameters), it will jump to usage; if the parameter is /? or help (you usually look at the help of a command, is it entered /? or help? This is done here just to make the script look more like a real program), and it will also jump to usage. Here we can also use negative form to represent "not equal". For example: if not "%1"=="" goto usage, it means that if the input parameter is not empty, it will jump to usage (in fact, it will be meaningless to do this. The usage is introduced here, and I can't control so much, haha.) Isn't it very simple? In fact, it will be understood after translating into Chinese.
(2) There is a judgment. Let’s look at this sentence in Example 2:
if exist C:\Progra~1\Tencent\AD\*.gif del C:\Progra~1\Tencent\AD\*.gif
If those gif files exist, delete them. Of course, there is also example 4, which are the same. Note that the conditional judgment here is to judge existence, and of course it can also be judged not exist, such as the following sentence "If those gif files do not exist, exit the script": if not exist C:\Progra~1\Tencent\AD\*.gif exit. It's just one more not to indicate negative.
(3) Results judgment. I'd better take the example 5 (I didn't expect the script I wrote was so useful, haha):
masm % if errorlevel 1 pause & edit % link %
First assemble the source code, pause the display of error messages if it fails, and automatically enter the editing interface after pressing any key; otherwise, use the link program to connect the generated obj file. Here I will only introduce the areas related to the if command, and the & command will be discussed later. This usage is to first judge the return code after the execution of the previous command (also called the error code, the DOS program has the return code after running). If it matches the defined error code (the error code defined here is 1), then the corresponding operation is performed (the corresponding operation here is pause & edit % part).
In addition, like the other two uses, this usage can also indicate negative. In the form of negative, the meaning of the above three sentences is still expressed, and the code becomes:
masm % if not errorlevel 1 link % pause & edit %
Have you seen the essence? In fact, we just exchanged the commands executed after the result judgment. The effects of "if not errorlevel 1" and "if errorlevel 0" are equivalent, both of which indicate that the previous masm command was successfully executed (because it is an error judgment, and the return code is 0, 0 means that the error does not exist, that is, the masm execution is successful). Whether to add not here and whether to use 0 or 1 in the error code are two issues worth considering. Once the matching is unsuccessful, an error will definitely occur, so you must have a deep understanding. How to experience the profoundness? practise! Write a script yourself, and then write the situations with not and no not, and the return code is 0 or 1 to execute (why, is it troublesome? If you calculate the situation in the arrangement and combination, do you think it is troublesome? Is there something even more troublesome when introducing pipeline commands and combination commands later! Are you scared? Haha.), so that the difference between these two situations can be clearly seen from the execution results.
This method of using errorlevel results is the most difficult usage of the if command, but it is also the most useful usage. If you don’t know how to use errorlevel to judge the return code, if you want to achieve the same effect, you must use else to represent the "otherwise" operation, which is more troublesome. The above code must become:
masm % if exist % link % else pause & edit %
The only way to say these three usages of the if command is said is very simple, but it may not be so easy to use when applying it. It is mainly a matter of proficiency. Some friends may be a little surprised. Why didn't I give an introduction to the usage of the following three lines? It is because the following three lines are the explanation of its own usage in the help of the if command. Anyone can see it with just one "if /?", and I don't need to talk too much here; the more important reason is that I don't think this introduction is clearly, and people who read it may not understand it, so I use my understanding of the if command above to introduce it. It must be noted that the formats of these three usages are different and cannot be changed, but they can actually be interchanged (I think that in essence, these three usages are based on judgment. Philosophy teaches us to learn to see the essence of things through phenomena!). Friends who are interested can study it yourself.
IF [NOT] ERRORLEVEL number do command IF [NOT] string1==string2 do command IF [NOT] EXIST filename do command
8、call
Friends who have studied assembly or C must know what the call instruction means, and the meaning here is actually the same. In a batch script, the call command is used to call another batch script from one batch script. See Example 8 (the default three script file names are, and):
:
……
CALL 0
……
:
……
ECHO %IPA%.%1 >
……
CALL
:
for /f "tokens=1,2,3 delims= " %%i in (%1) do call %%i %%j %%k
Have you seen anything wrong? Didn't see it? It’s right if you don’t see it, but there is actually nothing wrong. How can you see it! From the above two scripts, you can get the following information: 1. Script calls can be flexibly used, circularly used, and repetitively used. 2. Parameters can be used when script calls! I won’t say much about the first point. You should know the smart ones at a glance. Let’s talk about the second point here.
In this case, the parameter 0 is followed by. The effect during execution is actually to replace the parameter %1 in the case with 0. In the middle, the parameters are followed by (a file, which can also be used as parameters). The effect during execution is to use three variables in each line (it doesn't matter if you don't understand it here, you will understand it after learning the for command), corresponding to the substitution %%i, %%j and %%k. The parameter call here is very flexible and needs to be carefully experienced when using it. During the beginner's study, you can first learn to call scripts. As for the use of script parameters, you will naturally have a deeper understanding in the later learning. This is because when you can flexibly use batch scripts, how to write code more streamlined, more perfect and more efficiently naturally includes the scope of consideration. At this time, you will find that adding parameters directly when calling scripts can double the code efficiency. By the way, the scripts above are all part of the virus. In the subsequent tutorials, you will have the opportunity to see the true face of this virus.
Learn batch processing! Can you learn (three)
Does that mean that there are at least two batch script files in the same directory (there is only one who do you call?)? Haha, pay attention, this sentence is wrong! ! Only one can still call it--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- See Example 9 (default script file name):
net send %1 This is a call example.
call
The combination of these two sentences will naturally not have a good effect, because there is only one machine to send a message, who is afraid of whom? I'll give you a gift! But if there are 100 machines that execute at the same time, and each machine opens 10 and sends a message to a target machine at the same time, haha. The function of call here is to call itself, and after executing the previous net send command, then call itself, achieving the purpose of loop execution.
Give a very interesting script, and friends who are interested can experiment. Example 10 (the default script file name is):
call
Be sure to execute it in the DOS window, otherwise you will only see a window flashing by and you will not see the final result. After execution, when the script is executed 1,260 times, don’t forget to think about why! Love is sometimes like this script. Once it falls into a dead cycle, the final result is unexpected. It’s just love, you will never wait until you are cycled for no reason so many times. Maybe the reminder of love is aborted on the third time.
9、find
This is a search command used to search for specific strings in files, and is usually used as a preview for conditional judgments (how did I suddenly remember these four words?). The case where this command is used alone is relatively rare in batch processing because it has no practical significance. Or use example 3 to illustrate:
@echo off netstat -a -n > type | find "7626" && echo "Congratulations! You have infected GLACIER!" del pause & exit
First use the netstat command to check whether there is a default port 7626 in Glacier and save the result to it. Then use the type command to list the content, and then search for the string "7626" in the listed content. If you find that there is, it will prompt that you have hit the glacier, otherwise you will exit. Look, the find command is actually that simple, but one thing you must note: if you do not use the type command to list the content, but use the find command to directly find "7626" (find "7626" && echo "Congratulations! You have infected GLACIER!"), you must give the absolute path (I tried it. Find does not have a default path, which is the function of the current path, and it must be specified manually. Maybe I was wrong, welcome to correct it). Because there is a sentence in the help of the find command: If there is no specified path, find will search for text typed or generated by another command. The "another command" here naturally refers to the type command.
As for the other parameters of the find command, such as v, n, i, etc., friends who are interested, please study it yourself. This is already a DOS learning content, so I will not introduce it here. Some more subtle uses of the find command and other commands (some are simply amazing), will be introduced in the subsequent tutorials, I hope to pay attention to it.