& [...] Command1 & Command2 is used to separate multiple commands in one command line. Cmd.exe run the first command, and then run the second command.
&& [...] Command1 && Command2 is only used to run the command after the symbol && Previous command succeeds. Cmd.exe runs the first command, and then runs the second command only if the first command runs successfully.
|| [...] Command1 | | Command2 only in Symbols | | The previous command failed before it was used to run the symbol | | The following command. Cmd.exe runs the first command, and then runs the second command only if the first command fails to run successfully (receives an error code greater than 0).
All of the above are official help information (quoted in Ntcmds.chm), it is best to use GOOGL search to download a copy.
Command summary:
& generally reads "and".
Detailed parameters:
1. First Talk about &
"To separate multiple commands in a command line," the official explanation is actually from the interpreter's point of view. If you're on a person's terms, it should be "connect multiple commands".
For example I want to output Hello and world 2 lines of text, with "&" to connect two echo commands.
Copy CodeThe code is as follows:
@echo off
echo Hello & Echo World
Pause
The most common problem with "&" in use is variable latency, which can be referred to in the batching for statement from getting started to mastering the parts of this knowledge point.
2. Say errorlevel
ERRORLEVEL consists of two words, an error, a level (rank), and if you combine the two words, the literal meaning is actually "number of errors" or "error type".
If the command executes successfully, then the "Number of errors" is naturally zero. If execution fails, then the "Number of errors" is nonzero.
About ERRORLEVEL's posts:
Effect of Batch internal command on error return code ERRORLEVEL
Some speculation on ERRORLEVEL in batch processing
3. Commands that execute symbols before they are executed successfully
Output Hello and World 2 lines of text and connect two echo commands with "&&".
Copy CodeThe code is as follows:
@echo off
echo Hello && echo World
Pause
The result is exactly the same as the "&" link two command, "&" and "&&" function?
The answer is: they function completely differently, and the same result is just a coincidence.
The function of "&" is simply to link multiple commands, and "&&" is to determine if the command before the symbol is executed successfully, thus deciding whether to execute the command after the symbol.
Here is an example to illustrate:
Copy CodeThe code is as follows:
C:\users\helloworld\desktop>start "" "111.txt" && echo Hello
The system cannot find the file 111.txt.
C:\users\helloworld\desktop>start "" "111.txt" & Echo Hello
The system cannot find the file 111.txt.
Hello
Open a nonexistent file with the start command because the file does not exist, so the start command fails, and the command "echo Hello" with the "&&" link is not executed, and the "&" link is executed.
This feature of "&&" makes it a special conditional operator.
4. command to execute symbol before execution of symbol failed
The negative operation of && is | |.
For example:
Copy CodeThe code is as follows:
C:\users\helloworld\desktop>start "" "111.txt" | | echo Hello
The system cannot find the file 111.txt.
Hello
The connector in Bat