简体   繁体   English

Windows命令行通过引号

[英]windows command line pass through quotes

I have a command that I want to pass into a batch file that contains quotes. 我有一个命令想要传递到包含引号的批处理文件中。 What i want to do is pass in the following parameter as a single parameter into the bat file: 我想要做的是将以下参数作为单个参数传递到bat文件中:

set parameter=-c myfirstparameter -p "also my first parameter"

C:\mybat.bat parameter

Unfortunatly this ends up being: 不幸的是,最终结果是:

%1 = -c
%2 = myfirstparameter
etc

What I want is 我想要的是

%1 = -c myfirstparameter -p "also my first parameter"

You cannot do what you want. 你不能做你想做的。 Batch uses [space], [comma], [semicolon], [equal], [tab], and [0xFF] as token delimiters when parsing arguments. 在解析参数时,Batch使用[space],[逗号],[分号],[等于],[tab]和[0xFF]作为标记定界符。 If you want a token delimiter literal within an argument value, then the delimiter must be quoted. 如果要在参数值中使用标记定界符文字,则必须用引号引起来。 It is impossible to escape a token delimiter. 逃脱令牌定界符是不可能的。 Therefore it is impossible to have a single parameter that has both quoted and unquoted token delimiter literals. 因此,不可能有一个同时带有引号和未引号的令牌定界符文字的参数。

Presumably the top code in your question is wrong. 大概您问题中的最高代码是错误的。 In order to get the results you describe, you must have 为了获得您描述的结果,您必须具备

set parameter=-c myfirstparameter -p "also my first parameter"
C:\mybat.bat %parameter%

The best way to handle such a situation is to pass your parameter by reference, which requires a modification to your mybat.bat. 处理这种情况的最佳方法是通过引用传递参数,这需要对mybat.bat进行修改。

Instead of retrieving the value via %1 , you must enable delayed expansion and use !%1! 您必须启用延迟扩展并使用!%1! ,而不是通过%1检索值!%1!

@echo off
setlocal enableDelayedExpansion
set "argument1=!%1!"

With such an arrangement, you would indeed use the following to call the script: 通过这种安排,您确实可以使用以下代码来调用脚本:

set parameter=-c myfirstparameter -p "also my first parameter"
C:\mybat.bat parameter

Here is a working example of using a temp file ( as found here ) to pass the parameter: 这是一个使用临时文件( 在此处找到 )传递参数的工作示例:

FIRST.BAT 第一个BAT

echo -c myfirstparameter -p "also my first parameter">dummy.txt
second.bat

SECOND.BAT 第二个BAT

set /p param=<dummy.txt
echo %param%

Here is the raw output: 这是原始输出:

C:\temp\batchtest>first

echo -c myfirstparameter -p "also my first parameter" 1>dummy.txt

second.bat

set /p param= 0<dummy.txt

echo -c myfirstparameter -p "also my first parameter"
-c myfirstparameter -p "also my first parameter"

C:\temp\batchtest>

You need to quote the parameter. 您需要引用该参数。 Please try these in a simple, test .bat script. 请在一个简单的测试.bat脚本中尝试这些。

echo %*
echo %1
echo %~1
set IT=%~1
echo %IT%

set IT=%*
echo %IT%
echo %IT:~1,-1%

c:> y.bat "-p arf "asdfasdf asdfasdf"" c:> y.bat“ -p arf” asdfasdf asdfasdf“”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM