Execute multiple commands with 1 line in Windows commandline?
Solution 1:
Yes there is. It's &.
&&
will execute command 2 when command 1 is complete providing it didn't fail.
&
will execute regardless.
Solution 2:
If you want to execute multiple commands with 1 line, where you are starting the commands with start
, for example, if you wanted to execute a command like this:
start "" netsh dump && pause
Then, you need to do it in 2 steps (one-line solution is at the end of this answer).
First, write the commands to a temporary batch file (in this case, you can use &
or &&
):
echo netsh dump ^&^& pause ^&^& exit>foobar.cmd
-or-
echo netsh dump ^& pause ^& exit>foobar.cmd
Note that you need to "escape" each of the "&"s
(ampersands) with a "^"
to allow them to be treated as ordinary characters in the echo
command.
Alternatively, you can create the temporary batch file with a text editor, such as Notepad.
Then, use start
to start the batch file:
start "" foobar.cmd
-or-
start "" "temporary foobar.cmd"
Note: The empty pair of double-quote marks is for the
"Title"
that will be shown in the title-bar of the command window thatstart
will open. This"Title"
argument is technically an optional argument tostart
, but it is actually required, if the command thatstart
will run is double-quoted. For instance, in the second example:
start "" "temporary foobar.cmd"
if you leave out the empty pair of double quote marks like this:
start "temporary foobar.cmd"
then
start
will open a new command window, and use"temporary foobar.cmd"
as the new command window"Title"
, and nothing will be executed in the new command window.)
If you want start
to wait for the batch file to complete (after the pause
is dismissed), before start
completes, then you need to add the /w
switch to the start
command:
start "" /w foobar.cmd
You can put this all together on one line and even remove (delete) the temporary batch file (foobar.cmd
):
echo netsh dump ^&^& pause ^&^& exit>foobar.cmd && start "" /w foobar.cmd && del foobar.cmd
-or-
echo netsh dump ^& pause ^& exit>foobar.cmd & start "" /w foobar.cmd & del foobar.cmd
Note that if you are going to delete the temporary batch file, you need to run start
with the /w
switch, otherwise, the temporary batch file will probably be deleted before it has a chance to run.
Solution 3:
At least in MS-DOS 6.22 I used to use the key Ctrl+T to get a kind of paragraph symbol. This worked just like the & mentioned by Phoshi. This will only work however, if you have doskey.exe running.
Solution 4:
Yes use &
!
%command1% & %command2%
runs both commands
%command1% && %command2%
runs the first command, if that worked run the second command
%command1% || %command2%
runs the first command, if that failed run the seconds command
See here