When I run the command REG Query HKLM /k /F "Command Processor" /s /e /c
on cmd
, I get this result:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Command Processor
End results: 2 match(s) found(s).
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)."
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.
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.
The findstr
command is used to filter out the summary line End of search:
(this might be adapted according to your system locale/language).
Note that the array is no longer available after the endlocal
command.