简体   繁体   English

批处理reg查询变量的键

[英]Batch reg query key to variable

When I run the command REG Query HKLM /k /F "Command Processor" /s /e /c on cmd , I get this result: 当我在cmd上运行命令REG Query HKLM /k /F "Command Processor" /s /e /c时,得到以下结果:

HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Command Processor

HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Command Processor

End results: 2 match(s) found(s). 最终结果:找到2个匹配项。

But in batch: 但批量:

@echo off & setlocal ENABLEEXTENSIONS
for /f "tokens=*" %%a in ('REG Query HKLM /k /F "Command Processor" /s /e /c') do set "MyPath=%%a"
echo The path string value is "%MyPath%"
pause

When I execute this, I only get the last line: 执行此操作时,我只会得到最后一行:

The path string value is "End results: 2 match(s) found(s)." 路径字符串值为“最终结果:找到2个匹配项”。

What is wrong? 怎么了? I would like to get the path keys on variables. 我想获取变量的路径键。

The problem is obvious: you are overwriting the value of MyPath in the for /F loop, then you are printing ( echo ) the final value/line. 问题很明显:您正在for /F循环中覆盖MyPath的值,然后打印( echo最终值/行。

To get all lines (any arbitrary number) you could do the following: 要获取所有行(任意数字),您可以执行以下操作:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
rem storing the path strings in `MyPath1`, `MyPath2`, etc.:
set /A count=0
for /F "delims=" %%A in (
    'REG Query HKLM /K /F "Command Processor" /S /E /C ^
    ^| findstr /L /B /V /C:"End of search: "'
) do (
    set /A count+=1
    set "MyPath!count!=%%A"
)
rem extracting the previously stored path strings:
echo Total number of path strings: %count%
for /L %%B in (1,1,%count%) do (
    echo The !count!. path string value is "!MyPath%%B!"
)
pause
endlocal

This constitutes a sort of array MaPath1 , MyPath2 , and so on, containing all matching path strings. 这构成了数组MaPath1MyPath2等,包含所有匹配的路径字符串。

The findstr command is used to filter out the summary line End of search: (this might be adapted according to your system locale/language). findstr命令用于过滤摘要行End of search:可能会根据您的系统区域设置/语言进行调整)。

Note that the array is no longer available after the endlocal command. 请注意,在endlocal命令之后该数组不再可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM