SoFunction
Updated on 2025-04-09

It’s easy to learn batch processing! Another good insight page 1/3

This is a technical tutorial. You can sincerely express your meaning in very simple words. As long as you can read, you can understand and learn knowledge. The purpose of writing this tutorial is to let every friend who has read these words remember one sentence: If love can make things simpler, then make it simpler! The way to read this tutorial is to be slow! Slowly, it’s like tasting a woman or a cup of tea, you will find that many things that were in front of you suddenly became very far away, while some things that were very far away suddenly returned to you. .
Let’s first give an overview of what batch processing is. I have not been able to give a suitable definition of batch processing so far---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Maybe you will "love batch processing unconditionally" because of this article, then my goal will be achieved---I am just to make you fall in love with it. I am so arrogant. What can you do? ? Really, love is sometimes so arrogant, there is no reason, and so shameless! real!
According to my understanding, the essence of batch processing is a collection formed by a bunch of DOS commands arranged in a certain order.
OK, never claver and get to business (leave gossiping back to the point). Batch processing, also known as batch script, is translated as BATCH in English. The first three letters taken by the batch file suffix BAT. Its composition has no fixed format, as long as the following is followed, it will be OK: each line can be regarded as a command, and each command can contain multiple subcommands, which starts from the first line and ends until the last line. The platform it runs on is DOS. Batch processing has a distinctive feature: it is easy to use, flexible, powerful, and has a high degree of automation. I don't want the tutorials I wrote are boring, because the problem of tangling with code (the content of batch processing is considered code, right?) is boring, and few people can calm down when facing the code full of screens. So I will use many simple and practical examples to let friends who read this tutorial experience the charm of batch processing, feel its quirky personality, and fall in love with batch processing without realizing it (dream, why is it love again? What is the relationship between batch processing and love? Answer: No!). Let’s talk about “gossip”: To learn batch processing well, you must have a solid foundation in DOS! Of course, a flexible mind is also a very important aspect.
Example 1. First give a simple batch script to make everyone familiar with it. Save the following lines of commands and then execute them (there will be only the code in the text, and the saving and execution methods are similar):
ping  > 
ping  >> 
ping  >> 
ping  >> 
ping  >> 
ping  >> 
ping  >> 
ping  >> 
exit
Can you understand it? Isn't it easy? But its function is very practical. After executing this batch, you can create a file named on your current disk. The information recorded in it can help you quickly find the fastest QQ server, thus staying away from the painful process of "transferring from the server". The meaning of > here is to put the things obtained by the previous command in the place given later. The function of >> is the same as >. The difference is to append the result to the result obtained by the previous line, specifically the next line, and the result obtained by the previous line command will be retained, which can make the file larger and larger (think about how to destroy it??). By the way, this batch processing can also be combined with other commands to make it a complete automated way to judge the server speed. After execution, it will directly display the fastest server IP. Isn’t it great? This will be introduced in detail later.
Example 2: Give another outdated example ():
@echo off
if exist C:\Progra~1\Tencent\AD\*.gif del C:\Progra~1\Tencent\AD\*.gif

Why is this an outdated example? It's very simple, because almost no one uses QQ with ads now (KAO, my QQ also shows the friends' measurements!!!), so it is almost useless. But its role was once impossible to be underestimated: delete QQ ads and keep the dialog box clean. The address used here is the default installation address of QQ, and the default batch file name is, of course you can modify it yourself according to the situation. The if command is used in this script, so that it can achieve the effect of timely judgment and deletion of advertising images. You just need not close the DOS window after the command is executed, and do not press CTRL+C to forcefully terminate the command, and it will keep monitoring whether there are advertising images (QQ will continue to check whether its ads have been deleted). Of course this script takes up a little memory for you, haha.
Example 3: Use batch scripts to check whether it is in the Glacier. The script content is as follows:
@echo off
netstat -a -n > 
type  | find "7626" && echo "Congratulations! You have infected GLACIER!"
del  
pause & exit
The netstat command is used here to check the status of all network ports. You only need to know the ports used by common *s to easily determine whether they have been planted in the glacier. However, this is not certain, because the default port of Glacier 7626 can be completely modified by people. What we introduce here is only the methods and ideas. What is introduced here is that if the methods and ideas are changed slightly, it will become a script that can check other *s. After adding parameters, ports and information list files, it will become a script that automatically detects all *s. Haha, isn't it very satisfying? The script also uses the combination command && and pipeline command|, which will be introduced in detail later.
Example 4: Automatically clear system garbage through batch processing, and the script is as follows:
@echo off
if exist c:\windows\temp\*.* del c:\windows\temp\*.* 
if exist c:\windows\Tempor~1\*.* del c:\windows\Tempor~1\*.* 
if exist c:\windows\History\*.* del c:\windows\History\*.* 
if exist c:\windows\recent\*.* del c:\windows\recent\*.* 
Save the above script contents and automatically delete the system garbage every time it is turned on. There are two points to note here: 1. DOS does not support long file names, so the Tempor~1 thing appears; 2. You can change it according to your actual situation to make it meet your requirements.
How about it? After reading this, are you already interested in batch scripts? Have you found that you have slowly fallen in love with this thing? Don’t be too happy too early. Love is not a simple thing. It may bring you happiness and happiness, and of course it can also make you want to jump off a building in pain. If you know it's difficult and dare to continue, I am convinced YOU! Keep working hard, maybe in the end you may not get true love (it is really possible, everyone who has loved knows), but you can experience the whole process of love, which is it. Sour, bitter and spicy, God knows whether there is any sweetness.
Why is batch processing related to love? It’s not that I’m bored, nor that I’m much more interesting about writing like this. There are two reasons: First, batch processing and love have many similarities. In some places, I can’t explain them clearly in "professional" jargon (I don’t doubt my ability to express, but the things themselves are not easy to explain clearly). I said = didn’t say it, but used the metaphor of love that everyone on earth knows (what is love? How do I know it!!), maybe your heart will be bright all of a sudden, and you will get twice the result with half the effort, so why not do it? Secondly, I am not in good condition during this period, with cold, fever, headache, and nose congestion, but mainly emotionally devastated people, which makes people feel very annoyed. When I was writing the tutorial, I sighed a few words and said that everyone thought they had bought dog skin plaster, which could be omitted (maybe it really worked out----it wouldn't make you fall asleep while watching, and I kowtowed my head to find me to reimburse my medical expenses). Maybe in the next tutorial, you will also see the heroes written by senior Jin such as Yang Guo and Zhang Wuji.
Friends who have read the first chapter must have a preliminary impression of batch processing and know what it is used for. But do you know the essence of using batch processing? It’s actually very simple: be flexible! There is nothing that cannot be done, only unexpected things. This is a bit different from love, because the world of love is the world of two people, wishful thinking is not called love (supplement: that is called unrequited love. nonsense!), but batch processing is a paradise for one person. You can do whatever you want, and there is no realm that you cannot reach!
Batch processing looks messy, but it is so logical that it is definitely not lower than other programming languages ​​(such as assembly). If the script you write is a mess, although each line of command is correct, you may not get the result you want from the beginning to the end. Maybe it is a screen Bad command or fail name. This has something in common with love: to run according to the steps, missing or increasing steps may lead to unwanted results. I believe there are no friends who are in love who are not sure of this sentence. My love batch, the output result is not Bad command or fail name. The screen shows this: 'Your love' is not an internal or external command, nor is it a runnable program or batch file. Then the cursor keeps flashing, waiting for the next wrong input.
Starting from this chapter, we will introduce the commonly used commands in batch processing from a shallow to a deeper perspective. Many common DOS commands have such wide applications in batch scripts. They are the BODY part of batch scripts, but batch processing is more flexible and diverse and more automated than DOS. To learn batch processing well, DOS must have a relatively solid foundation. Here we will only talk about some relatively rarely used (relatively speaking) DOS commands, and we will not introduce them frequently used commands such as COPY, DIR, etc. (These seemingly simple commands are actually very complicated, and I am afraid that I can't explain them clearly!).
Example 5: Let’s look at an example first. This is a very interesting script, a small and practical thing that fully reflects the characteristics of batch processing "automation". Let me first introduce the origin of this script: Everyone knows the process of on-boarding of the assembly program (MASM). First, assemble and connect the source code, and then execute it. There are many links in the process that require inputting a lot of things, which is very troublesome (only friends who have experienced it understand). How to make this process simple? When we were designing the assembly course, I was "forced" to write this script, which was very pleasant to use, haha. Check out the script content:
@echo off 
::close echo
cls
::clean screen 
echo This programme is to make the MASM programme automate 
::display info
echo Edit by CODERED 
::display info
echo Mailto me : qqkiller***@ 
::display info
if "%1"=="" goto usage 
::if input without paramater goto usage
if "%1"=="/?" goto usage 
::if paramater is "/?" goto usage
if "%1"=="help" goto usage 
::if paramater is "help" goto usage 
pause 
::pause to see usage
masm % 
::assemble the .asm code
if errorlevel 1 pause & edit % 
::if error pause to see error msg and edit the code
link % & %1 
::else link the .obj file and execute the .exe file
:usage 
::set usage
echo Usage: This BAT file name [asm file name] 
echo Default BAT file name is  
::display usage
Don’t be scared by this pile of things first, calm down and take a closer look (recalling how the first paragraph of Chapter 1 was written!!). The explanation of each line of command has been given. The content after the two colons is explained by the E-text (you don’t have to worry about those who are afraid of E-text. They are all very easy and you can understand it at a glance. If you really don’t understand it, you won’t look up the dictionary. Are you so lazy?) will not be displayed when the script is executed and will not have any effect. There is a colon at the beginning of the fifth line at the end, which is not a typo! The specific functions will be discussed in detail later. In this script, masm and link are assemblers and linkers, and must be in the current directory along with the edit program and the source code you want to edit (of course, this script, nonsense!). Using this batch script, manual input can be reduced to the maximum possible possibility. During the whole process, you only need to press the Enter key a few times to realize the automatic conversion from assembly source code to executable exe file, and it has intelligent judgment function: if there is an error in the source code during assembly (assembly unsuccessful), the error message will be automatically paused, and the editing source code interface will be automatically entered; if the source code assembly is successful, the generated exe file will be automatically executed after the connection. In addition, due to the simplicity and flexibility of batch processing commands, this script is also good to improve, and simple modification can be in line with the computer-based habits of different friends. Friends who are studying compilation, please don’t forget to have an internship!
The following commands appear in this script: @, echo, ::, pause, : and goto, % and if. And this chapter will tell about these commands.
1、@
Everyone is familiar with this symbol. How can it run into batch processing for email? Haha, it’s not its fault. Batch processing is inseparable from it, otherwise it would be imperfect. Its purpose is to make the execution window not display the command itself (what a twisted sentence!). Haha, in layman's terms, if it is available at the beginning of the line, the commands on this line will not be displayed. In Example 5, in the first line of @echo off, the function of @ is to prevent the script from displaying the subsequent echo off part when executing. Do you understand now? Still don't understand it very much? It doesn't matter, after reading the introduction of echo commands, you will naturally understand.
2、echo 
In Chinese, it means "feedback" and "echo". It is actually a switch command, which means that it has only two states: on and off. So there are two commands: echo on and echo off. Execute the echo command directly to display the current echo command status (off or on). Execution of echo off will turn off echo. All commands following it do not display the command itself, only the result after execution is displayed, unless the echo on command is executed. In Example 5, the @ command and the echo off command on the first line are combined to achieve two purposes: not display the echo off command itself, and not display the command itself in the subsequent lines. It's indeed a bit messy, but if you practice it, you can get it in 3 minutes, and you won't get it back!
Another usage of the echo command: You can use it to display information! For example, the second to last line in five, Default BAT file name is  will be displayed in the window after the script is executed, while the echo command itself is not displayed (why??).
Another usage of the echo command is 2: You can edit text files directly. Example 6:
echo nbtstat -A 192.168.0.1 > 
echo nbtstat -A 192.168.0.2 >> 
echo nbtstat -A 192.168.0.3 >> 
The method of editing the above script content is to directly enter the command line and press it for each line. Finally, a file will be generated in the current directory, and the result will be obtained by executing it directly.
3、::
The function of this command is very simple. It is an annotation command, which is equivalent to the rem command in batch scripts. The content behind it is not displayed when executed and does not work, because it is just annotation, it only increases the readability of the script, similar to /*……*/ in C language. All people on earth can understand it, so I won’t say much.
4、pause
In Chinese, it means "pause" (check on your workman), and I have always thought it is the simplest command in batch processing, simple and practical. Its function is to pause the current program process and display a line of information: Please press any key to continue... In Example 5, this command was used twice. The first function was to let the user see the program information clearly, and the second was to display the wrong assembly code information (in fact, it was not that it wanted to display, but that the masm program was temporarily stopped when displaying the error message so that you can see clearly where your source code was wrong).
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 mentally retarded. 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⏺.bat -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 one, parameter two…. 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 you can also use negative form to represent "not equal", for example: if not "%1"=="" goto usage, which means that if the input parameter is not empty, it will jump to usage (in fact, it will be meaningless to do this. Here, the usage is introduced, it won't be too 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, the commands executed after the result judgment were just exchanged. The effects of "if not errorlevel 1" and "if errorlevel 0" are equivalent, both representing the successful execution of the previous masm command (because it is an error judgment, and the return code is 0, 0 means negative, which means that this 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 a "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.
123Next pageRead the full text