检索命令输出变量时,命令中有空格 [英] Retrieve command output to variable when command has spaces
问题描述
我读过的,我可以使用Windows批处理文件为
抢命令的输出,并把它放在一个变量,像这样不同的地方:
FOR / F %% G IN('富命令')DO SET FOO = %%摹
大。所以,我的 foo的命令
其实就是 C:\\ Program Files文件\\富\\ foo.bat
,这是存储在在 FOO_BAT
变量。它需要一个参数栏
值为等等等等='foobar的'等等
。所以我尝试:
对于那些SET FOO = %% G /˚F%% G IN(''嗒嗒%FOO_BAT%-bar等等等等='foobar的')
我得到一个可爱的 C:\\程序无法识别...
。我也没有办法可以避开在命令的空间。
如果要复制这一点,创建一个简单的 C:\\ Program Files文件\\富\\ foo.bat
包含文件刚刚下,然后运行在上面的行一个批处理文件。
呼应FooBar的
如何捕捉命令的输出(需要参数)作为Windows批处理变量,如果命令有空格,其路径?
- 是的,我试图把引号
%FOO_EXE%
。 - 是的,我已经试过把一切在一个单独的变量,并使用一个变量在
的
。 - 请试试这个的与我告诉我它的工作原理之前提供的参数。参数的presence改变了一切。
FOR / Fdelims =%% G IN('CALLC:用空格\\富\\路径。蝙蝠,等等等等='foobar的'嗒嗒)并设置富= %%摹
试试这个。是的,我测试了它。 Foo.bat什么也不做更多的则只是回声%1。
I've read in various places that I can use Windows batch file for
to grab the output of a command and put it in a variable, like this:
FOR /F %%G IN ('foo-command') DO SET FOO=%%G
Great. So my foo-command
is actually C:\Program Files\Foo\foo.bat
, and this is stored in the FOO_BAT
variable. And it takes a parameter bar
with value blah blah='foobar' blah
. So I try that:
FOR /F %%G IN ('%FOO_BAT% -bar "blah blah='foobar' blah"') DO SET FOO=%%G
I get a lovely 'C:\Program' is not recognized...
. Nothing I can do can get around the space in the command.
If you want to reproduce this, create a simple C:\Program Files\Foo\foo.bat
file containing just the following, and then run the line above in a batch file.
echo FooBar
How can I capture the output of a command (which takes parameters) as a Windows batch variable, if the command has spaces in its path?
- Yes, I've tried putting quotes around
%FOO_EXE%
. - Yes, I've tried putting everything in a separate variable and using that single variable in the
FOR
. - Please try this with the parameters I supplied before telling me it works. The presence of the parameters changes everything.
FOR /F "delims=" %%G IN ('CALL "C:\path with spaces\foo.bat" "blah blah='foobar' blah"') do set foo=%%G
Give this a try. And yes I tested it. Foo.bat does nothing more then just echo %1.
这篇关于检索命令输出变量时,命令中有空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!