Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 如何使用逗号分隔符在批处理文件中调用参数_Batch File - Fatal编程技术网

Batch file 如何使用逗号分隔符在批处理文件中调用参数

Batch file 如何使用逗号分隔符在批处理文件中调用参数,batch-file,Batch File,我想用逗号分隔的参数调用批处理文件。我怎样才能做到这一点 我想要这个例子 我有一个text.bat脚本: @echo off set test=%1 echo Sample %test% batch. 我希望按如下方式运行批处理: c:\text.bat this,is,sample Sample this,is,sample batch. 我期待着这样的结果: c:\text.bat this,is,sample Sample this,is,sample batch. 你知道我怎样

我想用逗号分隔的参数调用批处理文件。我怎样才能做到这一点

我想要这个例子

我有一个text.bat脚本:

@echo off
set test=%1
echo Sample %test% batch.
我希望按如下方式运行批处理:

c:\text.bat this,is,sample
Sample this,is,sample batch.
我期待着这样的结果:

c:\text.bat this,is,sample
Sample this,is,sample batch.
你知道我怎样才能做到这一点吗


谢谢。

哇!我不知道逗号是这样的

你有两个选择

您可以使用以下脚本:

@echo off
set test=%~1
echo Sample %test% batch.
@echo off
set test=%*
echo Sample %test% batch.
并使用以下工具运行它:

C:\text.bat "this,is,test"
C:\text.bat this,is,test
%~1
表示第一个不带引号的参数。引号将逗号分隔的列表分组为单个参数

您可以使用以下脚本:

@echo off
set test=%~1
echo Sample %test% batch.
@echo off
set test=%*
echo Sample %test% batch.
并使用以下工具运行它:

C:\text.bat "this,is,test"
C:\text.bat this,is,test

%*
表示键入的命令行参数。

在批处理文件中,除了空格外,参数分隔符还有逗号、分号和等号,因此没有直接的方法。唯一的选择是将参数括在引号中:
c:\text.bat“this,is,sample”
并将参数加上%%1以消除引号:
set test=%1

谢谢您的评论。对不起,我是新来的。我刚刚编写了一个循环,将%9之外的所有参数都转换为一个变量,但是我遇到了问题,因为一个参数是
a,b
,但是
%*
是最好的解决方案。谢谢。不仅
,而且
=
,因为您的答案添加了一些有价值的内容,即逗号、分号和等号用作参数分隔符。(这向我解释了为什么
shift
要拆分逗号分隔的字符串。)另一个答案没有这样说。但是,您说原始海报的唯一选择是使用
%~1
。事实并非如此;另一个选项(另一个答案提到)是使用
%*
。在这种情况下,逗号分隔的列表是否用引号括起来并不重要。@Alan:如果你读了这个问题,OP谈论的是“一个参数”,即一个参数。另一个答案要求使用所有参数,因此,例如,无法以相同的形式给出多个参数,如
c:\text.bat中的“this,is,sample”“second,param,with,commas”
。唯一的方法是将每个参数括在引号中,然后使用
%~1
%~2
。@Aacini,我明白了。