Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape percent signs in given variables

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>

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!

Cheers, Martin

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

Thanks for help, Martin

like image 565
CmdQuestion Avatar asked Feb 05 '13 22:02

CmdQuestion


People also ask

How do you escape the percent sign?

If you want to escape percent sign in String's format method, you can use % twice ( %% ). As you can see, we have used %% to escape percent symbol in 10% .

How do you escape a percentage sign in Python?

Using %% Character to Escape Percent Sign in Python To print percentage sign, we can escape using the percentage sign twice instead of once. See the code below. By using the percentage sign twice ( %% ), we can overcome the error.

How do you escape printf?

Escape sequences begin with a backslash '\' rather than an asterisk '*'. For example, a new-line character will be printed as '\n'; a non-standard character will be printed as a blackslash followed by three octal digits.


1 Answers

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!

EDIT: Clean solution

@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

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

like image 57
jeb Avatar answered Oct 03 '22 06:10

jeb