在Windows批处理文件在一行中的多个命令 [英] Multiple commands on a single line in a Windows batch file

查看:118
本文介绍了在Windows批处理文件在一行中的多个命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Unix中,我们可以把多个命令在这样的一行:

In Unix, we can put multiple commands in a single line like this:

$ date ; ls -l ; date

我在Windows尝试过类似的事情:

I tried a similar thing in Windows:

 > echo %TIME% ; dir ; echo %TIME

但它打印的时间,并且不执行命令 DIR

我怎样才能做到这一点?

How can I achieve this?

推荐答案

使用:

echo %time% & dir & echo %time%

这是从内存,相当于分号分隔的庆典等UNIXy炮弹。

This is, from memory, equivalent to the semi-colon separator in bash and other UNIXy shells.

还有&放大器;&安培; (或 || ),它仅执行第二个命令,如果第一次成功(或失败),但单个号&安培; 是你在找什么位置

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

这是在命令行中需要的。如果你正在做这个脚本中,你可以使用 SETLOCAL

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

这篇关于在Windows批处理文件在一行中的多个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆