如何在 Windows CMD 中的一行中运行两个命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8055371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:54:51  来源:igfitidea点击:

How do I run two commands in one line in Windows CMD?

windowsbatch-filecommand-linecmd

提问by flybywire

I want to run two commands in a Windows CMD console.

我想在 Windows CMD 控制台中运行两个命令。

In Linux I would do it like this

在 Linux 中,我会这样做

touch thisfile ; ls -lstrh

How is it done on Windows?

它是如何在 Windows 上完成的?

回答by djdanlib

Like this on all Microsoft OSes since 2000, and still good today:

自 2000 年以来在所有 Microsoft 操作系统上都是这样,今天仍然很好:

dir & echo foo

If you want the second command to execute only if the first exited successfully:

如果您希望第二个命令仅在第一个成功退出时执行:

dir && echo foo

The single ampersand (&) syntax to execute multiple commands on one line goes back to Windows XP, Windows 2000, and some earlier NT versions. (4.0 at least, according to one commenter here.)

在一行上执行多个命令的单与号 (&) 语法可以追溯到 Windows XP、Windows 2000 和一些早期的 NT 版本。(至少 4.0,根据这里的一位评论者的说法。)

There are quite a few other points about this that you'll find scrolling down this page.

向下滚动此页面,您会发现很多关于此的其他要点。

Historical data follows, for those who may find it educational.

历史数据如下,对于那些可能会发现它具有教育意义的人。

Prior to that, the && syntax was only a feature of the shell replacement 4DOS before that feature was added to the Microsoft command interpreter.

在此之前,&& 语法只是 shell 替换 4DOS 的一个功能,之后该功能被添加到 Microsoft 命令解释器中。

In Windows 95, 98 and ME, you'd use the pipe character instead:

在 Windows 95、98 和 ME 中,您可以使用管道字符代替:

dir | echo foo

In MS-DOS 5.0 and later, through some earlier Windows and NT versions of the command interpreter, the (undocumented) command separator was character 20 (Ctrl+T) which I'll represent with ^T here.

在 MS-DOS 5.0 及更高版本中,通过一些早期的 Windows 和 NT 版本的命令解释器,(未记录的)命令分隔符是字符 20 (Ctrl+T),我将在这里用 ^T 表示。

dir ^T echo foo

回答by Raihan

A quote from the documentation:

文档中的引用:

Using multiple commands and conditional processing symbols

You can run multiple commands from a single command line or script using conditional processing symbols. When you run multiple commands with conditional processing symbols, the commands to the right of the conditional processing symbol act based upon the results of the command to the left of the conditional processing symbol.

For example, you might want to run a command only if the previous command fails. Or, you might want to run a command only if the previous command is successful.

You can use the special characters listed in the following table to pass multiple commands.

  • & [...]
    command1 & command2
    Use to separate multiple commands on one command line. Cmd.exe runs the first command, and then the second command.

  • && [...]
    command1 && command2
    Use to run the command following && only if the command preceding the symbol is successful. Cmd.exe runs the first command, and then runs the second command only if the first command completed successfully.

  • || [...]
    command1 || command2
    Use to run the command following || only if the command preceding || fails. Cmd.exe runs the first command, and then runs the second command only if the first command did not complete successfully (receives an error code greater than zero).

  • ( ) [...]
    (command1 & command2)
    Use to group or nest multiple commands.

  • ; or ,
    command1 parameter1;parameter2
    Use to separate command parameters.

使用多个命令和条件处理符号

您可以使用条件处理符号从单个命令行或脚本运行多个命令。当您使用条件处理符号运行多个命令时,条件处理符号右侧的命令将根据条件处理符号左侧的命令的结果执行操作。

例如,您可能只想在前一个命令失败时运行命令。或者,您可能只想在前一个命令成功时运行命令。

您可以使用下表中列出的特殊字符来传递多个命令。

  • & [...]
    command1 & command2
    用于在一个命令行上分隔多个命令。Cmd.exe 运行第一个命令,然后是第二个命令。

  • && [...]
    command1 && command2
    仅当符号前面的命令成功时才用于运行 && 后面的命令。Cmd.exe 运行第一个命令,然后仅当第一个命令成功完成时才运行第二个命令。

  • || [...]
    command1 || command2
    用于运行 || 后面的命令 仅当 || 前面的命令 失败。Cmd.exe 运行第一个命令,然后仅当第一个命令未成功完成(收到大于零的错误代码)时才运行第二个命令。

  • ( ) [...]
    (command1 & command2)
    用于分组或嵌套多个命令。

  • ; or ,
    command1 parameter1;parameter2
    用于分隔命令参数。

回答by manojlds

&is the Bash equivalent for ;( run commands) and &&is the Bash equivalent of &&(run commands only when the previous has not caused an error).

&;(run commands)&&的 Bash 等价物,是&&(run commands only when the previous has not导致错误)的等价物。

回答by scrappedcola

You can use & to run commands one after another. Example: c:\dir & vim myFile.txt

您可以使用 & 一个接一个地运行命令。例子:c:\dir & vim myFile.txt

回答by TroodoN-Mike

If you want to create a cmd shortcut (for example on your desktop) add /k parameter (/k means keep, /c will close window):

如果要创建 cmd 快捷方式(例如在桌面上),请添加 /k 参数(/k 表示保留,/c 将关闭窗口):

cmd /k echo hello && cd c:\ && cd Windows

回答by SSi

You can use callto overcome the problem of environment variables being evaluated too soon - e.g.

您可以使用call来克服过早评估环境变量的问题 - 例如

set A=Hello & call echo %A%

回答by sambul35

A number of processing symbolscan be used when running several commands on the same line, and may lead to processing redirection in some cases, altering output in other case, or just fail. One important case is placing on the same line commands that manipulate variables.

在同一行上运行多个命令时可以使用许多处理符号,并且在某些情况下可能导致处理重定向,在其他情况下改变输出,或者只是失败。一个重要的情况是将操作变量的命令放在同一行上。

@echo off
setlocal enabledelayedexpansion
set count=0
set "count=1" & echo %count% !count!

0 1

As you see in the above example, when commands using variables are placed on the same line, you must use delayed expansion to update your variable values. If your variable is indexed, use CALL command with %% modifiers to update its value on the same line:

正如你在上面的例子中看到的,当使用变量的命令放在同一行时,你必须使用延迟扩展来更新你的变量值。如果您的变量已编入索引,请使用带有 %% 修饰符的 CALL 命令在同一行更新其值:

set "i=5" & set "arg!i!=MyFile!i!" & call echo path!i!=%temp%\%%arg!i!%%

path5=C:\Users\UserName\AppData\Local\Temp\MyFile5

回答by dpp.2325

cmd /c ipconfig /all & Output.txt

cmd /c ipconfig /all & Output.txt

This command execute command and open Output.txtfile in a single command

此命令Output.txt在单个命令中执行命令并打开文件

回答by SNAFUBAR

So, I was trying to enable the specific task of running RegAsm(register assembly) from a context menu. The issue I had was that the result would flash up and go away before I could read it. So I tried piping to Pause, which does not work when the command fails (as mentioned here Pause command not working in .bat scriptand here Batch file command PAUSE does not work). So I tried cmd /kbut that leaves the window open for more commands (I just want to read the result). So I added a pausefollowed by exitto the chain, resulting in the following:

所以,我试图RegAsm从上下文菜单中启用运行(注册程序集)的特定任务。我遇到的问题是,结果会在我阅读之前闪现并消失。所以我尝试通过管道传输到Pause,当命令失败时它不起作用(正如这里提到的Pause 命令在 .bat 脚本中不起作用,这里的批处理文件命令 PAUSE 不起作用)。所以我尝试过,cmd /k但这会让窗口打开以获取更多命令(我只想读取结果)。所以我在链中添加了一个pause后跟exit,结果如下:

cmd /k C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm.exe "%1" /codebase \"%1\" & pause & exit

cmd /k C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm.exe "%1" /codebase \"%1\" & pause & exit

This works like a charm -- RegAsm runs on the file and shows its results, then a "Press any key to continue..." prompt is shown, then the command prompt window closes when a key is pressed.

这就像一个魅力 - RegAsm 在文件上运行并显示其结果,然后显示“按任意键继续...”提示,然后在按下某个键时关闭命令提示窗口。

P.S. For others who might be interested, you can use the following .reg file entries to add a dllfile association to .dll files and then a RegAsm command extension to that (notice the escaped quotes and backslashes):

PS 对于可能感兴趣的其他人,您可以使用以下 .reg 文件条目将 dllfile 关联添加到 .dll 文件,然后向其添加 RegAsm 命令扩展名(注意转义引号和反斜杠):

[HKEY_CLASSES_ROOT\.dll]
"Content Type"="application/x-msdownload"
@="dllfile"

[HKEY_CLASSES_ROOT\dllfile]
@="Application Extension"

[HKEY_CLASSES_ROOT\dllfile\Shell\RegAsm]
@="Register Assembly"

[HKEY_CLASSES_ROOT\dllfile\Shell\RegAsm\command]
@="cmd /k C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm.exe \"%1\" /codebase \"%1\" & pause & exit"

Now I have a nice right-click menu to register an assembly.

现在我有一个不错的右键单击菜单来注册程序集。

回答by PryroTech

In order to execute two commands at the same time, you must put an & (ampersand) symbol between the two commands. Like so:

为了同时执行两个命令,您必须在两个命令之间放置一个 &(与号)符号。像这样:

color 0a & start chrome.exe

Cheers!

干杯!