如何串联批处理文件的命令行参数? [英] How to concatenate command line args for a batch file?

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

问题描述

我目前正在尝试将命令行参数与此连接起来:

I'm currently attempting to concatenate my command line arguments with this:

for %%a in (%*) do set "subject=%subject% %%a"

例如,如果我跑步

my.bat subject line here

应该将我的主题变量设置为此处的主题行",并保留空格.但是,当前运行之后,我的subject变量设置为最后一个单词.我得到的主题值为这里".

it should set my subject variable to "subject line here", preserving the spaces. However, currently after the run, my subject variable is set to the last word. I get a subject value of " here."

如何正确连接命令行参数?

How to concatenate the command line arguments right?

推荐答案

您不能这样做:

SET subject=%*

或者启用延迟扩展,以便在解析过程中不会替换环境变量.

Alternatively enable delayed expansion so that the environment variables don't get substituted during parsing.

Setlocal EnableDelayedExpansion
for %%a in (%*) do set subject=!subject! %%a
echo %subject%

请参见>%variable%与!variable之间的差异!批处理文件中以获取更多信息.

see Difference between %variable% and !variable! in batch file for more info.

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

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