简体   繁体   English

Windows 批处理文件中一行上的多个命令

[英]Multiple commands on a single line in a Windows batch file

In Unix, we can put multiple commands in a single line like this:在 Unix 中,我们可以将多个命令放在一行中,如下所示:

$ date ; ls -l ; date

I tried a similar thing in Windows:我在 Windows 中尝试过类似的事情:

 > echo %TIME% ; dir ; echo %TIME

But it printed the time and doesn't execute the command dir .但它打印了时间并且不执行命令dir

How can I achieve this?我怎样才能做到这一点?

Use:采用:

echo %time% & dir & echo %time%

This is, from memory, equivalent to the semi-colon separator in bash and other UNIXy shells.根据记忆,这相当于bash和其他 UNIXy shell 中的分号分隔符。

There's also && (or || ) which only executes the second command if the first succeeded (or failed), but the single ampersand & is what you're looking for here.还有&& (或|| )仅在第一个成功(或失败)时才执行第二个命令,但单个&符号&是您在此处寻找的。


That's likely to give you the same time however since environment variables tend to be evaluated on read rather than execute.但是,这可能会给您相同的时间,因为环境变量往往在读取而不是执行时进行评估。

You can get round this by turning on delayed expansion:您可以通过打开延迟扩展来解决这个问题:

pax> cmd /v:on /c "echo !time! & ping 127.0.0.1 >nul: & echo !time!"
15:23:36.77
15:23:39.85

That's needed from the command line.这是命令行所需要的。 If you're doing this inside a script, you can just use setlocal :如果您在脚本中执行此操作,则只需使用setlocal

@setlocal enableextensions enabledelayedexpansion
@echo off
echo !time! & ping 127.0.0.1 >nul: & echo !time!
endlocal

Can be achieved also with scriptrunner也可以使用scriptrunner实现

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

Which also have some features as rollback , timeout and waiting.其中还有一些功能,如回滚、超时和等待。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM