title: Escape percent signs in given variables
body: <p>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.</p>
  
  <p>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 "&amp;", "(", ")" and "%". The string must be echoed to a file without quotes afterwards. So I had the idea to escape the poison characters tripled:</p>
  
  <pre><code>@echo off &amp; setlocal ENABLEEXTENSIONS
  SET AlbumArtist=%1
  CALL :EscapePoisonChars %AlbumArtist% AlbumArtist_VDN
  
  SET "FlacHyperLink==hyperlink^("file://%AlbumArtist_VDN%"^;"LossLess"^)")
  echo %FlacHyperLink%
  echo %AlbumArtist_VDN%
  
  endlocal &amp;GOTO:EOF
  
  :EscapePoisonChars
  @echo off &amp; setlocal ENABLEEXTENSIONS
  SET TmpString=%1
  SET TmpString=%TmpString:&amp;=^^^&amp;%
  SET TmpString=%TmpString:(=^^^(%
  SET TmpString=%TmpString:)=^^^)%
  endlocal&amp;SET %2=%TmpString:~1,-1%&amp;GOTO :EOF
  </code></pre>
  
  <p>When I call my script above I get the expected output - apart from the missing percent sign:</p>
  
  <pre><code>G:\YAET\20130204_Work&gt;TryAmper.bat "100% Rock &amp; Roll (7' UpMix)"
  =hyperlink("file://100 Rock &amp; Roll (7' UpMix)";"LossLess")
  100 Rock &amp; Roll (7' UpMix)
  
  G:\YAET\20130204_Work&gt;
  </code></pre>
  
  <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!</p>
  
  <p>Cheers, Martin</p>
  
  <p><strong>Edit</strong>
  Removed own code, see below Jeb's answer for clean solution.</p>
  
  <p>Thanks for help, Martin</p>