Search code examples
batch-fileregistrydos

Setting a REG QUERY result to a string


I have been trying to label the result from a REQ QUERY into a string. I have been attempting to have the path itself of the result to be displayed as a string value so I can use it later on.

I have made this in attempt to do so however have come up with some errors.

@ECHO off
REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print" /F "C:\\jdavies6" /S
set KEY_NAME="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print"
set VALUE_NAME=C:\\jdavies6

FOR /F "usebackq skip=2 tokens=1-3" %%A IN (`REG QUERY %KEY_NAME% /F "%VALUE_NAME%" /S`) DO (
    set ValueName=%%A
    set ValueType=%%B
    set ValueValue=%%C
)

@ echo %ValueName%
Pause 

I would like the script to set the key path at the value for me to use however it is setting the "end of search" as the values. This is because it is overwriting the set values 3 times for each line of the results, however I am unsure on how to rectify this?


Solution

  • This construct will set the variables only on the first pass through the loop.
    It depends on the variables being blank to begin with so initialise them before the loop if needed.

    if not defined ValueName  set ValueName=%%A
    if not defined ValueType  set ValueType=%%B
    if not defined ValueValue set ValueValue=%%C