逃生中给出的变量百分号 [英] Escape percent signs in given variables

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

问题描述

我的第一篇文章,大部分问题在这里使用这种友好提供了所学知识已经解决了。但现在我有一个关于输入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:\YAET\20130204_Work>TryAmper.bat "100% Rock & Roll (7' UpMix)"
=hyperlink("file://100 Rock & Roll (7' UpMix)";"LossLess")
100 Rock & Roll (7' UpMix)

G:\YAET\20130204_Work>

我知道的百分比可自行进行转义。所以,%%通常会导致一个文字%。但它不是我能够找到一个工作更换程序百分号因为CMD始终间$ P $点作为一个变量,并试图将其展开。这是完全错误的方向来处理这个问题,或者变量扩展只是误会?任何提示欢迎!谢谢!

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!

干杯,马丁

修改
删除了自己的code,见下文杰布的答案清晰的解决方案。

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天全站免登陆