79

How can I execute multiple commands in the Windows commandline with just a single line?

So for example say I want to perform an SVN update and then copy all of the files to another location...

svn update; copy *.* c:\development\copy\

That doesn't work obviously. Is there a character or delimiter like ';' to perform something like this?

8 Answers 8

121

Yes there is. It's &.

&& will execute command 2 when command 1 is complete providing it didn't fail.

& will execute regardless.

4
  • 8
    & and && only work in Windows' cmd.exe - which is not DOS. Oct 30, 2009 at 15:21
  • 24
    'DOS' as a term is often used to describe cmd, and seeing as the op uses svn I highly doubt he's still using DOS. No need to be pedantic :)
    – Phoshi
    Oct 30, 2009 at 15:37
  • There: urlencode.blogspot.com/2009/06/vista-desktop-june-09.html He uses Windows XP. Certainly not DOS (it never looked that good ;))
    – Phoshi
    Oct 30, 2009 at 15:45
  • 9
    I think it's safe to assume that when anyone mentions "DOS", they really mean "Windows Command Prompt". I'm going to go out on a limb here and say that most people probably don't use DOS much these days. Thanks for the correction - it's just a bit unnecessary in this day and age. Nov 5, 2009 at 4:35
6

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 that start will open. This "Title" argument is technically an optional argument to start, but it is actually required, if the command that start 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.

2
  • Instead of /w one could put a short sleep before deleting the file. timeout 2 will do it, for example.
    – Antonio
    Aug 24, 2016 at 12:16
  • @kevin-fegan why do you need a temporary cmd file???
    – maxxyme
    Jul 10, 2019 at 7:34
4

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.

3

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

2
  • And also &, &&, and || aren't actually a command, it's part of batch. Not a command. Jul 12, 2018 at 7:53
  • And %command% can execute that string as a command, seen here Jul 12, 2018 at 7:55
1

If you are in powershell you can use ; to join commands.

0

In case you want to wrap the first command with an if but would still like the second command to execute regardless then then you have to wrap the if with a cmd /c as well like this:

cmd /c if exist abc. (rd /q abc) & echo hello

If you don't prefix the if with cmd /c then the whole command becomes part of if (which you may not want).

3
  • Or you could say (if exist abc. command₁) & command₂ Dec 7, 2016 at 18:30
  • @Scott I know compliments are a sin on these sites, but start "" cmd /c "(command₁) & (command₂)" is legit not answered anywhere on the web but is the only way to link most batch statements. Congrats to you.
    – John Kens
    Jul 22, 2018 at 23:46
  • 1
    I appreciate compliments / congratulations, but I don’t understand what you’re saying.  user138334 said, “In case you want to wrap the first command with an if but would still like the second command to execute regardless then then you have to wrap the if with a cmd /c as well”, and I responded with a way of doing it without cmd /c.  So why are you saying that the only way to link most batch statements is using cmd /c?  And why are you throwing start into the mix? Jul 23, 2018 at 1:28
-1

I found that using 'START' fixed multiple commands working (I'm running Windows 10 Home). For example:

START chrome.exe "page.web.com" & timeout 20
3
  • Answers should not contain commentary, like "Thanks for this", it distracts from your solution. You should consider editing this answer, so it simply answers the question, with as much detail as much but without the noise (i.e. the command that does not work)
    – Ramhound
    Dec 7, 2016 at 18:00
  • What he is looking for, is &, not start! Dec 8, 2016 at 22:53
  • What problem does this solve? What happens when you type the command line without start? Jul 23, 2018 at 1:31
-2

make a batch file with a short name ie bat.bat.

add cmd /c %1 
    cmd /c %2 
rem

and so on to 9 lines and save

then use it with double quotes

bat "command one" "command two" "command three" 

cmd /c will run each command and shut down for the next line not saying this is better than && just that it's easy.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .