转义给定变量中的百分号 [英] Escape percent signs in given variables

查看:41
本文介绍了转义给定变量中的百分号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一篇文章,大多数问题已经使用这里提供的友好知识解决了.但是现在我的想法已经用完了,又是一个关于在 cmd.exe 中处理有毒字符的问题.

My first post, most questions already solved using this friendly provided knowldge here. But now I run out of ideas, again with a question about handling of poison characters in cmd.exe.

让我们假设有一个用双引号括起来的给定字符串变量.大多数毒字符之前已经被普通字符取代,剩下的干扰脚本的是&"、("、)"和%".之后必须将该字符串回显到不带引号的文件中.所以我有了逃避毒字符三倍的想法:

Let's assume there is a given string variable enclosed in double quotes. Most poison characters has already been replaced by common chars before, the left ones disturbing the script are "&", "(", ")" and "%". The string must be echoed to a file without quotes afterwards. So I had the idea to escape the poison characters tripled:

@echo off & setlocal ENABLEEXTENSIONS
SET AlbumArtist=%1
CALL :EscapePoisonChars %AlbumArtist% AlbumArtist_VDN

SET "FlacHyperLink==hyperlink^("file://%AlbumArtist_VDN%"^;"LossLess"^)")
echo %FlacHyperLink%
echo %AlbumArtist_VDN%

endlocal &GOTO:EOF

:EscapePoisonChars
@echo off & setlocal ENABLEEXTENSIONS
SET TmpString=%1
SET TmpString=%TmpString:&=^^^&%
SET TmpString=%TmpString:(=^^^(%
SET TmpString=%TmpString:)=^^^)%
endlocal&SET %2=%TmpString:~1,-1%&GOTO :EOF

当我调用上面的脚本时,我得到了预期的输出——除了缺少百分号:

When I call my script above I get the expected output - apart from the missing percent sign:

G:YAET20130204_Work>TryAmper.bat "100% Rock & Roll (7' UpMix)"
=hyperlink("file://100 Rock & Roll (7' UpMix)";"LossLess")
100 Rock & Roll (7' UpMix)

G:YAET20130204_Work>

我知道百分比可以自己转义.所以%%"通常会导致单个文字%".但是我无法找到百分号的有效替换程序,因为 cmd 总是将其解释为变量并尝试扩展它.这是处理这个问题的完全错误的方向还是只是对变量扩展的误解?欢迎任何提示!谢谢!

I know that the percent can be escaped by itself. So "%%" will normally lead to a single literal "%". But it was not possible for me to find a working replace procedure for percent signs because cmd always interprets it as a variable and tries to expand it. Is this the complete wrong direction to handle this issue or just misunderstanding of variable expansion? Any hints welcome! Thanks!

干杯,马丁

编辑删除了自己的代码,请参阅下面 Jeb 对干净解决方案的回答.

Edit Removed own code, see below Jeb's answer for clean solution.

感谢您的帮助,马丁

推荐答案

好问题!
起初,是的,您甚至可以替换百分号,但不能在百分比扩展内,您需要在这里延迟扩展.

Nice question!
At first, yes you can replace even percent signs, but not within a percent expansion, you need a delayed expansion here.

Setlocal EnableDelayedExpansion
set tmpstr=!tmpstr:%=%%!

但是如果使用延迟扩展,则不再需要转义,因为延迟扩展是批处理解析器的最后阶段,所有字符都失去了任何特殊含义.
您只需要响应延迟扩展.

But if you use the delayed expansion, you don't need the escapes anymore, as the delayed expansion is the last phase of the batch parser and all characters lose any special meaning.
You only need to echo with delayed expansion.

Echo !tmpvar!

干净的解决方案

@echo off
setlocal DisableDelayedExpansion

REM * More or less secure getting the parameter
SET "AlbumArtist=%~1"

setlocal EnableDelayedExpansion
SET "FlacHyperLink==hyperlink("file://!AlbumArtist!";"LossLess")"

echo !FlacHyperLink!
echo !FlacHyperLink!> hugo.txt

您首先需要 disableDelayedExpansion,才能从 %1 获得感叹号.
之后,您应该切换到延迟扩展并在任何地方使用它.

You need disableDelayedExpansion first, to get even exclamation marks from %1.
After that, you should switch to delayed expansion and use it anywhere.

这篇关于转义给定变量中的百分号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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