用空格,双引号,管道批处理文件参数 [英] Batch file parameter with spaces, double quotes, pipes

查看:132
本文介绍了用空格,双引号,管道批处理文件参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要传递一个参数,包括管道和空间的批处理文件。由于空间,双引号在需要的时候通过它连接到的参数,我需要去掉那些双引号和回声的参数。通常情况下,使用〜会让我这样做,但我想讲讲我传递的具体参数会导致一个问题。如果我这样做:

I have a batch file that needs to be passed a parameter that will include pipes and spaces. Because of the spaces, double quotes need to be attached to the parameter when passing it in. I need to strip off those double quotes and echo the parameter. Normally, using the ~ would let me do this, but I think something about the specific parameters I'm passing in causes a problem. If I do this:

[test1.bat]

[test1.bat]

call test2.bat "Account|Access Level|Description"

[test2.bat]

[test2.bat]

echo %1
echo %~1

和运行test1.bat,我得到这样的输出:

And run test1.bat, I get this output:

"Account|Access Level|Description"
'Access' is not recognized as an internal or external command, operable program or batch file.

那么,如何去除双引号,仍然有一个可用的变量?

So how do I remove the double quotes and still have a usable variable?

推荐答案

您可以用延迟的扩展,因为它并不关心特殊字符。结果
唯一的问题是让参数的内容到一个变量,因为它只能通过百分号展开传递。结果
但是,在你的情况下,这个应该工作。

You could use delayed expansion, because it doesn't care about special characters.
The only problem is to get parameter content into a variable, as it can only transfer via a percent expansion.
But in your case this should work.

@echo off
setlocal DisableDelayedExpansion
set "str=%~1"
setlocal EnableDelayedExpansion
echo !str!

此言一出,我先禁用延迟的扩展,所以!而^不受%的扩张修改1

Remark, I disable first the delayed expansion, so the ! and ^ aren't modified by the expansion of %1

编辑:延迟扩展可以禁用或

setlocal DisableDelayedExpansion
setlocal EnableDelayedExpansion

如果启用,它增加了扩展变量的另一种方式(!变!,而不是%变量%),小学至prevent变量的括号块效应(在集描述/?)。

If enabled, it adds another way of extending variables (!variable! instead of %variable%), primary to prevent the parenthesis block effect of variables (described at set /?).

但随着扩张!变!也prevents任何进一步的解析的内容,因为推迟扩张一批行解析的最后阶段。

详细这是在说明
<一href=\"http://stackoverflow.com/questions/4094699/how-does-the-windows-command-inter$p$pter-cmd-exe-parse-scripts/4095133#4095133\">how确实窗口命令间preTER命令提示字符解析脚本

But the expansion with !variable! also prevents the content of any further parsing, because the delayed expansion is the last phase of batch line parsing.
In detail it is explained at how does the windows command interpreter cmd exe parse scripts

这篇关于用空格,双引号,管道批处理文件参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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