windows 带空格、双引号、管道的批处理文件参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5239291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:22:24  来源:igfitidea点击:

Batch file parameter with spaces, double quotes, pipes

windowsbatch-file

提问by SuperNES

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

And run test1.bat, I get this output:

并运行 test1.bat,我得到这个输出:

"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?

那么如何删除双引号并仍然有一个可用的变量呢?

采纳答案by jeb

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!

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

备注,我首先禁用延迟扩展,所以 ! 和 ^ 不会被 %1 的扩展修改

EDIT:The delayed expansion can be disabled or enabled with

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

setlocal DisableDelayedExpansion
setlocal EnableDelayedExpansion

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 /?).

如果启用,它会添加另一种扩展变量的方式(!variable!而不是%variable%),主要是为了防止变量的括号块效应(在 中描述set /?)。

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

但是扩展 with!variable!也阻止了任何进一步解析的内容,因为延迟扩展是批处理行解析的最后阶段。
详细解释 windows命令解释器cmd exe如何解析脚本

回答by Anders

@echo off
if "%~2"=="" (
    call %0 "Account|Access Level|Description" dummy
) ELSE (
    setlocal ENABLEEXTENSIONS
    for /F "tokens=*" %%A IN ("%~1") DO @echo.%%A
)

Not exactly pretty, but it works. Dealing with special characters is always a pain in batch files...

不完全漂亮,但它有效。在批处理文件中处理特殊字符总是很痛苦......