TenG
TenG

Reputation: 4004

Processing reg query output as a single line

I want to get some details from the Windows registry in a single line (one per key) format. But what I have tried so far gives me the details I want, but split over 2/3 lines, which makes post processing harder.

This is on Windows 10.

One option is to query the registry, so I run the following:

reg query hklm\system\currentcontrolset\enum /s /f "DeviceDesc"

This gives me output in the following format (snippet):

HKEY_LOCAL_MACHINE\system\currentcontrolset\enum\HID\VID_1A2C&PID_2124&MI_01&Col02\7&2a45f711&0&0001
    DeviceDesc    REG_SZ    @input.inf,%hid_device_system_control%;HID-compliant system controller

HKEY_LOCAL_MACHINE\system\currentcontrolset\enum\HID\VID_1A2C&PID_2124&MI_01&Col02\8&9a82e8&0&0001
    DeviceDesc    REG_SZ    @input.inf,%hid_device_system_control%;HID-compliant system controller

HKEY_LOCAL_MACHINE\system\currentcontrolset\enum\HID\VID_2149&PID_2117&MI_00\7&1e3fba77&0&0000
    DeviceDesc    REG_SZ    @input.inf,%hid_device_touch_screen%;HID-compliant touch screen

HKEY_LOCAL_MACHINE\system\currentcontrolset\enum\HID\VID_2149&PID_2117&MI_01\7&316fd6b5&0&0000
    DeviceDesc    REG_SZ    @input.inf,%hid_device_vendor_defined_range%;HID-compliant vendor-defined device

HKEY_LOCAL_MACHINE\system\currentcontrolset\enum\HID\VID_24AE&PID_2003&MI_00\8&456ad84&0&0000
    DeviceDesc    REG_SZ    @keyboard.inf,%hid.keyboarddevice%;HID Keyboard Device

The format is:

BLANK LINE
HKEY_LOCAL.....
    DeviceDesc .....

What I want is the HKEY_LOCAL... and DeviceDesc to appear on the same line of output text, so that I can use FIND/FINDSTR to get the complete info for the device I am interested in.

With the output as it stands, I cannot get the two piece of information together using DOS commands.

Is there a way to make DeviceDesc appear of the same line ?

I could write a Java/C# for this, but it seems overkill.

Upvotes: 3

Views: 2247

Answers (2)

John Kens
John Kens

Reputation: 1679

From what I understand, you want to combine each lines Key Path & Data. The easiest way to do this (From My knowledge) Is to grab each result of the FOR loop and use an IF statement to set the strings we will later combine.

Bellow you will find the script - In my case I just outputted the results to a document. This is fine and can be called later to with a TYPE statement inside of a FOR. However if you want to do something with these variables in the loop, just simply continue your code in-place of the ECHO [!Location! !Data!] >> Output.txt.

@ECHO OFF
@setlocal EnableDelayedExpansion

Set "RUN=0"
for /F "tokens=*" %%A in ('reg query hklm\system\currentcontrolset\enum /s /f "DeviceDesc"') DO (

    Rem | Grab & organize output variables to string.
    If "!RUN!"=="1" (

        Rem | Second Cycle
        Set "Data=%%A"
        ECHO !Location! !Data! >> Output.txt

        Rem | Restart Cycle
        Set "Data="
        Set "Location="
        Set "RUN=0"

    ) ELSE (

        Rem | First Cycle
        Set "Location=%%A"

        Set "RUN=1"
    )
)

Goto :EOF

Upvotes: 1

Mofi
Mofi

Reputation: 49085

The command REG has no options to define the output format.

A FOR loop can be used to concatenate registry key and the device description string value for output on one line. The entire output of FOR loop can next be filtered with command FINDSTR for the device description of interest:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
(for /F "tokens=1,2*" %%A in ('%SystemRoot%\System32\reg.exe query HKLM\System\CurrentControlSet\enum /s /f "DeviceDesc"') do if /I not "%%A" == "DeviceDesc" (set "RegKey=%%A") else echo !RegKey!    %%C) | %SystemRoot%\System32\findstr.exe /I /L /C:"HID-compliant touch screen"
endlocal

Please note that registry keys or description values containing one or more ! are not correct processed by this batch code because of enabled delayed environment variable expansion.

There are four spaces used to separate registry key from device description. It is of course also possible to use for example a horizontal tab.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • endlocal /?
  • findstr /?
  • reg /?
  • reg query /?
  • set /?
  • setlocal /?

Upvotes: 1

Related Questions