如何加入下面的批处理命令? [英] How to join the batch commands below?

查看:93
本文介绍了如何加入下面的批处理命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加入下面的命令,以便可以通过"int system(const char *)"来调用它们。在同一个会话中。

I want to join the commands below so they can be invoked by "int system( const char * )" within the same session.

set a=3
echo %a%

结果是:

3

但是,我无法使用任何字符获得与上述相同的结果{&,&&,|,||}加入两个意外的命令。

However, I couldn't get the same result as above using any of the characters {&, &&, |, ||} to join the two commands, which were unexpected.

set a=3 & echo %a%


set a=3 && echo %a%


set a=3 | echo %a%

所有结果 原来是:

All the results turned out to be:

%a%

那么如何正确加入两个批处理命令而不影响结果呢?请

So how to join the two batch commands properly without affecting the result, please?

推荐答案

在命令提示符下,一行命令设置a = 3& echo%a%也将返回%a%。

At a command prompt the one line command set a=3 & echo %a% will also return %a%.

这是因为命令解释器在读取行时扩展环境变量,而不是在执行行时。请参阅
在读取命令时发生环境变量扩展进一步说明以及如何在批处理文件中使用延迟扩展的示例。

This is because the command interpreter expands environment variables when the line is read, not when it is executed. See Environment variable expansion occurs when the command is read for a further explanation and an example of how to use delayed expansion in a batch file.

试试这个 -

int main()
{
    system("cmd /v:on /c \"set a=3 & echo !a!\"");
    return 0;
}


这篇关于如何加入下面的批处理命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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