在批量查询中使用变量

时间:2018-07-10 11:25:54

标签: batch-file

我为此感到困惑。我希望找到一个密钥然后将其删除。我已注释掉删除内容进行测试,因为它会产生错误,如“ reg查询”行所示。

@echo off

for /f  %%a in (' 
    reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server" ^|
    find "StartRCM"
') do (
     set "regs=%%a"
)

echo %regs%

set key="HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\%regs%"
echo %key%

REM reg delete %key% /f
reg query %key%

输出为:

C:\>test2.bat
StartRCM
"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\StartRCM"
ERROR: The system was unable to find the specified registry key or value.

C:\>

编辑1

C:\>Reg Query "HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server"

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server
    StartRCM    REG_DWORD    0x0
...

C:\>

1 个答案:

答案 0 :(得分:1)

@echo off

for /f  %%a in ('
    reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server" ^|
    find "StartRCM"
') do (
     set "regs=%%a"
)

echo "%regs%"

set "key=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server"
echo %key%

if defined regs reg delete "%key%" /v "%regs%" /f
reg query "%key%"

您的初始代码,并调整了StartRCM

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server
     StartRCM    REG_DWORD    0x0

从您的查询输出中:

  • 第一行是
  • StartRCM
  • REG_DWORD数据类型
  • 0x0数据