SoFunction
Updated on 2025-04-10

Another good batch bat learning tutorial page 4/5


To sum up: The function of the "|" command is to use the output of the previous command as the input of the next command.

“>、>>” 

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:
Copy the codeThe code is as follows:

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


Copy and paste the above code and generate a file in the current directory, with the contents as follows:

Copy the codeThe code is 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 >>.

“<、>&、<&” 

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.

“&”: 

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.


“&&”: 

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 and 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 it 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:\ >>  

“||”: 

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:

Copy code
@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 will definitely not be successful, so I continue to execute exit and will naturally exit, haha. So how to solve the problem given by the problem? See example 20:


Copy code
@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
Previous page12345Next pageRead the full text