CMD嵌套在参数双引号 [英] CMD nested double quotes in argument

查看:2155
本文介绍了CMD嵌套在参数双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有传递参数给一个批处理功能嵌套双引号的问题。

I am having problems with passing arguments to a batch function with nested double quotes.

下面是一个批处理文件的例子:

Here is an example of a batch file:

@SET path_with_space="c:\test\filenamewith space.txt"
@CALL :FUNCTION blaat1, "blaat2 %path_with_space%"
@GOTO :EOF

:FUNCTION
@echo off
echo arg 1: %~1
echo arg 2: %~2
echo arg 3: %~3
GOTO :EOF

输出是:

arg 1: blaat1
arg 2: blaat2 "c:\test\filenamewith
arg 3: space.txt""

我应该怎么做才能让 ARG 2:blaat2C:\\测试\\ filenamewith space.txt
请注意,我无法调整功能或改变%path_with_space%。我只能控制什么被传递给函数。

What should I do to make arg 2: blaat2 "c:\test\filenamewith space.txt"? Note that I cannot adjust the function or change the %path_with_space%. I can only control what is passed to the function.

推荐答案

像dbenham说,这似乎是不可能逃脱不带引号中的一个参数的空间。结果
但如果你知道怎么的接收的函数得到的参数也可能是可行的。结果
然后,你可以转院通过逃脱延迟变量参数,变量将不会在通话扩大,它只是在功能上进行扩展。结果
和它的必要的参数将在函数变量被分配,但是这可能是一个很好的和可读code中的情况

Like dbenham said, it's seems impossible to escape a space in a parameter without quotes.
But it could be possible if you know how the receiver function gets the parameters.
Then you could tranfer the parameter via an escaped delayed variable, the variable will be expanded not in the call, it will be expanded just in the function.
And it's necessary that the parameters will be assigned in the function to variables, but this could be the case in a good and readable code.

setlocal EnableDelayedExpansion 
set path_with_space="c:\test\filenamewith space.txt"
@CALL :FUNCTION blaat1, "blaat2 ^!path_with_space^!"
GOTO :EOF

:FUNCTION
@echo off
echo arg 1: %~1
echo arg 2: %~2
echo arg 3: %~3
GOTO :EOF 

输出是:

arg 1: blaat1
arg 2: blaat2 "c:\test\filenamewith space.txt"
arg 3:

编辑:Soltution用的分批注入

Soltution with batch injection

这样,即使推迟扩张应始终被禁止。结果
但你现在需要的参数是如何在功能扩展。

This works even when delayed expansion should be always disabled.
But then you need to now how the parameters are expanded in the function.

@echo off
set path_with_space="c:\test\filenamewith space.txt"
CALL :FUNCTION 1 2 ""^^"&call:inject:""
exit/b

:inject
set arg1=blaat1
set arg2=blaat2 %path_with_space%
set arg3=none
exit /b

:FUNCTION
@echo off
set "arg1=%~1"
set "arg2=%~2"
set "arg3=%~3"
echo arg 1: %arg1%
echo arg 2: %arg2%
echo arg 3: %arg3%
GOTO :EOF

这篇关于CMD嵌套在参数双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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