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

在Linux中我会这样做

touch thisfile ; ls -lstrh

在Windows上是怎么做的呢?


当前回答

在windows 10中,你也可以使用scriprunner:

ScriptRunner.exe -appvscript demoA.cmd arg1 arg2 -appvscriptrunnerparameters -wait -timeout=30 -rollbackonerror -appvscript demoB.ps1 arg3 arg4 -appvscriptrunnerparameters -wait -timeout=30 -rollbackonerror

它允许你在一行上启动几个命令,你可以连续运行它们,也可以不等待彼此,你可以设置超时和错误回滚。

其他回答

我尝试在同一个窗口中有两个ping,它是在同一行上的串行命令。完成第一个命令后,执行第二个命令。

解决方案是在Windows 7命令提示符中结合start /b。

像往常一样启动,不带/b,并在单独的窗口中启动。

在同一行中用于启动的命令是:

start /b command1 parameters & command2 parameters

无论如何,如果您希望解析输出,我不建议使用这种方法。 我注意到输出在命令的输出之间被打乱了。

文档中的一段话:

来源:微软,Windows XP专业产品文档,命令shell概述 另外:Windows CMD命令的A-Z索引

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.

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

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

0 1

正如您在上面的例子中看到的,当使用变量的命令被放在同一行时,您必须使用延迟展开来更新变量值。如果你的变量被索引了,使用CALL命令和%%修饰符来更新它在同一行的值:

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

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

再举一个例子:例如,当我们使用gulp构建系统时,而不是

Gulp -默认>构建

Gulp构建-构建构建文件夹

Gulp watch -开始文件监视

Gulp dist- build dist-folder

我们可以用一行代码来实现:

cd c:\xampp\htdocs\project & gulp & gulp watch

是的,有。&。

如果命令1没有失败,&&将在命令1完成时执行命令2。

无论如何都会执行。