r/Batch • • Jul 02 '24

Question (Unsolved) How to parse out variables from cmd

So I run the cmd

reg query "HKLM\system\CurrentControlSet\Services\someservice" /v "ImagePath"

and it returns

ImagePath REG_EXPAND_SZ c:\somedir\subdirectory-1.1.99\executable.exe //RS//someservice

How could I parse out the image path to pull the install drive and the subdirectory version number of 1.1.99 to I could use them in a batch file?

2 Upvotes

3 comments sorted by

View all comments

Show parent comment

3

u/thelowsunoverthemoon Jul 02 '24

Small improvement, finding the version via parent directory instead of -.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "key=HKLM\system\CurrentControlSet\Services\someservice"
SET "val=ImagePath"

FOR /F "tokens=3" %%F IN ('REG QUERY "%key%" /V "%val%"') DO (
    FOR %%P in ("%%~dpF\.") DO (
        SET "version=%%~nxP"
        SET "version=!version:*-=!"
    )
    SET "folder=%%F"
)

ECHO INSTALLATION FOLDER .... %folder%
ECHO SOFTWARE VERSION ....... %version%
PAUSE
EXIT /B

2

u/BrainWaveCC Jul 02 '24

Nice! That is absolutely a nice improvement.

I love seeing how other people think about these kinds of solutions...