【问题标题】:How do you expand a REG_EXPAND_SZ in a batch file?如何在批处理文件中展开 REG_EXPAND_SZ?
【发布时间】:2016-07-26 07:56:36
【问题描述】:

我希望能够扩展使用reg query <KEY> /v <VALUE> 检索到的REG_EXPAND_SZ 的值。

如何扩展包含环境变量的环境变量(用%包围)? 比如我想将x中包含的%systemroot%\system32\config展开为C:\Windows\system32\config

【问题讨论】:

  • 就用吧。这就是你需要做的。
  • @Noodles 这不是真的。例如,如果环境变量x%UserProfile%\Documents,其中echo '%x%'的结果为'%UserProfile%\Documents',则不能使用dir "%x%"或类似的操作。该字符串包含一个文字百分比符号,通常不会被特别视为环境变量,即使它是需要的。
  • 你也可以使用延迟扩展
  • @SomethingDark 这到底是怎么做的?如果它有效,它将是一个很好的答案。我只能看到使用延迟扩展来扩展单个变量名称,而不是包含 % 和其他项目的名称。

标签: batch-file registry environment-variables


【解决方案1】:

使用call set

:: This code is for a batch file, as expansion works differently.
set x=%%systemroot%%\system32\config
:: '%x%' is now '%systemroot%\system32\config'
call set "y=%x%"

callparse its code block twice,所以call set "y=%x%" 将首先扩展为set "y=%systemroot%\system32\config",然后当调用该集合时,它(在我的系统上)扩展为set "y=C:\Windows\system32\config"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    • 2012-06-17
    • 1970-01-01
    相关资源
    最近更新 更多