Escape percent signs in given variables

前端 未结 1 1597
借酒劲吻你
借酒劲吻你 2020-12-03 19:36

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 character

相关标签:
1条回答
  • 2020-12-03 20:22

    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.

    0 讨论(0)
提交回复
热议问题