Chaining Commands
In Command Prompt, the symbols &, &&, and || let you run multiple commands on a single line.
You can write things like “create a folder, then move into it” or “show a message and exit if the copy fails” without an IF statement, which is why chaining is common in both batch files and interactive use.
Chaining Operators
| Symbol | Name | Behavior |
|---|---|---|
& | Unconditional | Runs the next command regardless of the result of the previous one |
&& | AND (on success) | Runs the next command only if the previous one succeeded (exit code 0) |
|| | OR (on failure) | Runs the next command only if the previous one failed (exit code other than 0) |
Success or failure is determined by the exit code (ERRORLEVEL). For details on exit codes, see the ERRORLEVEL article.
Running Commands in Sequence with &
& runs the next command whether the previous one succeeded or failed.
command1 & command2
echo %DATE% & echo %TIME%
If there is a space right before &, as in echo %DATE% , that space becomes part of the output. When the exact output matters, remove the space: echo %DATE%& echo %TIME%.
Running Only on Success with &&
&& runs the next command only if the previous one succeeded. Use it to chain steps that depend on the previous step having finished.
mkdir work && cd work
If mkdir fails (for example, no permission, or a folder or file with the same name already exists), cd work is not run. Note that if the work folder already exists, running this command again makes mkdir fail, so cd work is skipped as well.
copy "report.txt" "D:\backup\report.txt" && del "report.txt"
This prevents the accident of deleting the original file when the copy failed.
Running Only on Failure with ||
|| runs the next command only if the previous one failed. It is useful for showing error messages or aborting a process.
ping -n 1 192.168.1.1 | find "TTL=" >nul || echo No response
Exiting a Batch File Early
In batch files, the pattern || exit /b 1 is commonly used to stop processing when an error occurs.
@echo off
cd /d "D:\project" || exit /b 1
copy "config.ini" "config.bak" || exit /b 1
echo Preparation complete
exit /b 1 ends the batch file with exit code 1. For details, see
Combining && and ||
Combining the two lets you branch on success and failure.
tasklist | find "notepad.exe" >nul && echo Running || echo Not running
find returns exit code 0 when it finds a matching line and 1 when it does not, so it is well suited to checking whether a process or text exists. For other errors (such as a missing file) it returns 2, but || treats every non-zero code as a failure, so the “Not running” side also runs on errors.
It is tempting to read A && B || C as “B if A succeeds, otherwise C”, but C also runs if A succeeds and B fails.
copy "a.txt" "b.txt" && findstr "xyz" "nofile.txt" >nul 2>&1 || echo Copy failedIn the example above, even though the copy succeeds, the second command (here, a findstr on a nonexistent file) fails, so “Copy failed” is printed. When you need strict branching, use
Order of Evaluation
&& and || have the same precedence and are evaluated from left to right. A command that is skipped because its condition was not met passes the previous result along.
cd /d C:\Windows || echo A && echo B
When cd succeeds, echo A is skipped, but the “success” result is carried over, so echo B runs. Also, & has lower precedence than && and ||: A & B && C is interpreted as “run A, then run B && C”. The pipe | and redirection bind more tightly than all of these operators. Complex combinations easily behave unexpectedly, so use grouping with parentheses, described next.
Grouping with Parentheses
Enclosing multiple commands in parentheses ( ) treats them as a single unit.
copy "data.csv" "backup\" >nul || (
echo Backup failed
exit /b 1
)
del "data.csv"
If you write A && (on success) || (on failure), the failure block also runs when the last command of the success block fails. Grouping only the failure handling with ||, as above, is the safer form.
(echo Date: %DATE% & echo Time: %TIME%) > log.txt
When you attach a redirect to the whole group, as in (echo Date: %DATE% & echo Time: %TIME%) > log.txt, all standard output from inside the group is saved to the file (add 2>&1 to save error output too). For redirection, see the redirect article.
Splitting a Long Chain Across Lines
When a chain gets long and hard to read, put ^ at the end of a line. This escapes the line break, so the next line continues the same command line. Do not put spaces after the ^ or a blank line directly after it.
mkdir "work" ^
&& cd "work" ^
&& echo Work folder is ready
For how to use ^, see the escape article.
Things to Watch Out For
A Variable Set on the Same Line Cannot Be Referenced There
cmd parses the entire line before running it, so if you reference a variable with %name% on the same line where you set it, it is replaced with the value from before the assignment.
set MSG=Hello & echo %MSG%
Because the whole line is parsed first, the value of MSG at parse time (empty if undefined in a batch file, or the literal %MSG% at the command line) is passed to echo, so Hello is not displayed. To work around this, enable setlocal EnableDelayedExpansion in a batch file (or cmd /v:on at the command line) and write !MSG!, or put the commands on separate lines. For details, see
Extra Space in the set Value
If you write set MSG=Hello & echo ..., the value of MSG becomes Hello (with a trailing space), because the space before & is included in the value. Enclose the assignment in double quotes: set "MSG=Hello" & echo ....
Chaining After an IF
Commands chained after the body of an IF with & or && also run only when the IF condition is true.
if exist "a.txt" echo Found & echo Processing
If a.txt does not exist, neither echo runs. Because the intent is hard to read, wrap multiple commands in parentheses.
Commands Like robocopy That Return Non-Zero on Success
can return a non-zero exit code even on success, such as 1 when files were copied (8 or higher means an error). For such commands,&& and || cannot judge the result correctly.
robocopy "src" "dst" /E
if %ERRORLEVEL% GEQ 8 echo Copy failed
Handling of Special Characters
& and | are special characters in Command Prompt. To display them as literal characters, escape them, as in echo A ^& B. Enclosing the text in double quotes also works, but echo "A & B" prints the quotes as well. Also, | (
|| (OR), and & and &&, are different operators, so do not confuse them.
Differences in PowerShell
&& and || are not available in Windows PowerShell 5.1 (they are available in PowerShell 7 and later). The syntax in this article describes Command Prompt (cmd.exe).
Practical Examples
Move to a Folder on Another Drive and List Its Contents
cd /d "D:\project" && dir /b
Create a Folder if It Does Not Exist
if not exist "logs" mkdir "logs"
In this case, IF is more appropriate than chaining. See also the file existence check article.
Continuing Even if a Command Fails
With &, the following command runs even if the previous one failed.
taskkill /im notepad.exe >nul 2>&1 & timeout /t 3 /nobreak >nul
Quiz
Summary
| Goal | Syntax |
|---|---|
| Run in order regardless of result | A & B |
| Run next only on success | A && B |
| Run next only on failure | A || B |
| Exit the batch file on failure | A || exit /b 1 |
| Treat several commands as a unit | (A & B) && C |
| Split a long chain across lines | ^ at line end |
&&and||are decided by the exit code of the preceding commandA && B || Cis not a substitute for if-else; useIFfor strict branching- A variable set on the same line cannot be referenced with
%var%unless you use delayed expansion - Commands such as
robocopy, which return non-zero on success, cannot be judged correctly with&&or||