怎么逃避!在变量的值?

时间:2015-10-04 22:39:15

标签: windows batch-file cmd escaping

我怎么能逃脱嵌入!在延迟扩展中使用的变量PL的值中的字符!PL!,它们不被解释为分隔符?

E.g。当%% P包含时纠正这个失败!

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
    )
)

编辑:保罗的适用性有限的解决方法:

function foo()
{
    $items = func_get_args();
    $value = array_pop($items);
    $array = [];
    $arrayPtr = &$array;

    foreach ($items as $element) {
        $arrayPtr[$element] = null;
        $arrayPtr = &$arrayPtr[$element];
    }
    $arrayPtr[$element] = $value;

    return $array;
}

var_dump(foo('arg1', 'argx', 'argz', 'value'));

1 个答案:

答案 0 :(得分:1)

我不知道如何搜索/替换!内的!var! !myvar:^^!=!!无法工作)但是您可以在var中显示!

@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
)

您可以像这样转置代码:

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
    )
)