Search code examples
batch-fileregistry

Is there a way return just a sub-key and not the whole path in Batch file?


I've created a batch file that query's the data in saves it into a CSV file. The data I'm searching for is saved as a sub-key in the registry (the values inside the sub-key are irrelevant). I have the following query:

reg query "HKCU\Software\Gizmo International\HCM\IPC Config" >>%userprofile%\desktop\info.csv

And this is the result:

HKEY_CURRENT_USER\Software\Gizmo International\HCM\IPC Config\g1322222_127.0.0.1 

But I just want to get the sub-key only: g1322222_127.0.0.1 (which is under \IPC Config) and not the whole path, then save it in a new cell in the CSV file.

The reason being that we have to search a large number of computers for the sub key (it is a unique number in every PC and it is saved under the sub-key \IPC Config) and build a database in Excel, but the long path is crowding the CSV file.

Can anyone please help me with this?


Solution

  • If you're looking for just the subkey name, and there's only one *, then you could use this:

    @Echo Off
    Set "HKCU=&H80000001"
    Set "RKEY=Software\Gizmo International\HCM\IPC Config"
    For /F "Tokens=2 Delims={}" %%A In (
        'WMIC Class StdRegProv Call EnumKey "%HKCU%"^,"%RKEY%" 2^>Nul^|Find "sNames"'
    ) Do (Echo %%~A)>>"%UserProfile%\Desktop\info.csv"
    Pause
    

    If there were more than one subkey, (* or you wanted the single returned name inside doublequotes for your csv output), then you'd ) Do Echo %%A>>"%UserProfile%\Desktop\info.csv"


    Edit

    If you want them each without quotes then you can use this modification:

    @Echo Off
    Set "HKCU=&H80000001"
    Set "RKEY=Software\Gizmo International\HCM\IPC Config"
    
    For /F "Tokens=2 Delims={}" %%A In (
        'WMIC Class StdRegProv Call EnumKey "%HKCU%"^,"%RKEY%" 2^>Nul^|Find "sNames"'
    ) Do Set "_=%%A" & >>"%UserProfile%\Desktop\info.csv" Call Echo %%_:"=%%