2010-08-12 42 views
3

我想传递一个空格分隔变量到像一个批处理文件:如何将空格分隔的变量传递给bat文件?

c:\applications\mi_pocess.bat A1 1AA 

当我运行echo %1mi_process返回A1

我怎么会去它承认A1 1AA是一个字符串?

我已经试过它环绕在我的外部软件双引号像

c:\applications\mi_pocess.bat + chr$(34) + A1 1AA + Chr$(34) 

echo %1现在返回"A1 1AA"(我不想在变量引号)

感谢

+0

你感谢2人,现在,他们已经回答了你的问题?如果是,请接受答案。 – 2013-03-07 01:50:19

回答

0

这是向批处理文件发送包含空格的值的唯一方法,因此如果不需要引号,则必须在批处理文件中将其删除。

3

我确定您知道%1,%2等等.bat内代表传递给命令行上.bat的编号参数。

如果您使用它们作为%~1,%~2等,则所有的周围引号将自动删除,如果它们在那里。

考虑这个space-in-args.bat来进行测试:

@echo off 
echo. %1 
echo. (original first argument echo'd) 
echo. 
echo. "%1" 
echo. (original first argument with additional surrounding quotes) 
echo. 
echo. %~1 
echo. (use %%~1 instead of %%1 to remove surrounding quotes, should there be) 
echo. 
echo. "%~1" 
echo. (we better use "%%~1" instead of "%%1" in this case: 
echo.  1. ensure, that argument is quoted when used inside the batch; 
echo.  2. avoid quote doubling should the user have already passed quotes. 
echo. 

运行:

space-in-args.bat "a b c d e" 

输出是:

"a b c d e" 
    (original first argument echo'd) 

""a b c d e"" 
    (original first argument with additional surrounding quotes) 

    a b c d e 
    (using %~1 instead of %1 removes surrounding quotes, should there be some) 

"a b c d e" 
    (we better use "%~1" instead of "%1" in this case: 
     1. ensure, that argument is quoted when used inside the batch; 
     2. avoid quote doubling should the user have already passed quotes. 

参见for /?(向下滚动接近年底)来找出关于更多这些转换。

+0

谢谢皮皮塔斯 - 这很好地解释它 – Mark 2010-08-13 06:55:42

1

要么你想在批处理文件使用%~1

echo %1 
echo %~1 

产量:

> test "A1 AA1" 
"A1 AA1" 
A1 AA1 

因为%~1删除从参数报价。

如果您永远不会期望超过一个参数,那么另一种选择是%*%*包含批处理文件的所有参数。这将使您避免使用此引号,但它也意味着,如果你想给另一个说法,不应该是你目前正在期待一个部分,你的运气了:

echo %* 

然后得出:

> test2 A1 AA1 
A1 AA1 
+0

谢谢Johannes。现在在我的处理中执行类似的操作 – Mark 2010-08-13 06:56:13

0

你应该使用%*获得除%0

sended所有参数,如果你调用一些批处理文件这样 call some bat 54 12 33

与代码

set params=%* 
echo Params goted : %params% 

则回复:

Params goted : 54 12 33