2

How may I escape embedded ! characters in the value of a variable PL used in delayed expansion !PL!, such that they are not interpreted as delimiters?

E.g. to remedy the failure of this when %%P contains ! .

FOR %%P IN (%input%\*.M3U) DO (
    echo Processing playlist "%%P"

    SETLOCAL ENABLEDELAYEDEXPANSION
    FOR /F "tokens=*" %%L IN ('type "%%P"') DO (
      SET PL=%%~pP%%L
      echo  Processing reference "!PL!"
    )
  ENDLOCAL
)

EDIT: Paul's limited-applicability workaround:

FOR %%P IN (%input%\*.M3U) DO (
    echo Processing playlist "%%P"

    FOR /F "tokens=*" %%L IN ('type "%%P"') DO (
      SET PL=%%~pP%%L
      SETLOCAL ENABLEDELAYEDEXPANSION
      echo  Processing reference "!PL!"
      ENDLOCAL
    )
)
ChrisJJ
  • 2,191
  • 1
  • 25
  • 38
  • 1
    possible duplicate of [How can I escape an exclamation mark ! in cmd scripts?](http://stackoverflow.com/questions/3288552/how-can-i-escape-an-exclamation-mark-in-cmd-scripts) – aschipfl Oct 05 '15 at 05:41

1 Answers1

1

I don't know how you can search/replace a ! inside a !var! (!myvar:^^!=!! doesn't work) but you can display ! in var like:

@echo off
(
echo !!myfolder
echo myfi!es
echo !234
)>sample.txt
for /f %%a in (sample.txt) do (
  set "myvar=%%a"
  setlocal enabledelayedexpansion
  echo !myvar!
  endlocal
)

You can transpose on your code like this:

for %%p in (%input%\*.m3u) do (
    echo processing playlist "%%p"

    for /f "tokens=*" %%l in ('type "%%p"') do (
      set pl=%%~pp%%l
      setlocal enabledelayedexpansion
      echo  processing reference "!pl!"
      endlocal
    )
)
Paul
  • 2,620
  • 2
  • 17
  • 27
  • You can replace it only with percent expansion. `echo %myvar:!=someThingElse%` – jeb Oct 05 '15 at 09:32
  • @Paul Thanks for the transpose workaround. Works fine. Still it would be useful to have a full solution. And thanks for the block redirect technique, which will hopefully improve my future SO CMD questions :) – ChrisJJ Oct 05 '15 at 11:14