0

How do I retrieve the string "C:\Program Files (x86)\Stupid\MS\" from the following input?

HKEY_CURRENT_USER\Software\Stupid\MS`    
     installpath    REG_SZ    C:\Program Files (x86)\Stupid\MS\`

please use windows bat file.

My way:

@echo off
Setlocal enabledelayedexpansion 
for /f "tokens=2,* delims=:" %%i in ('reg query "HKEY_CURRENT_USER\Software\Stupid\MS" /v "installpath"') do ( 
    echo %%i
) 
pause

the result is : \Program Files (x86)\Stupid\MS\

2 Answers 2

1
req query "HKEY_CURRENT_USER\Software\Stupid\MS" /v "installpath"

returns this on my xp (sorry I don't have anything more recent at the moment)

! REG.EXE VERSION 3.0

HKEY_CURRENT_USER\Software\Stupid\MS    
 installpath    REG_SZ    C:\Program Files (x86)\Stupid\MS

When using delim's default value (a space) what we actually need is the third and following tokens from the last line. "third and following" is tokens=3*. The third token will be available in %%i, the "and following" in %%j. This letter is automatically assigned.

Unfortunately the first line also has a third and following token, so we need to exclude the first line. We can do that by using the eol option to exclude lines starting with "!". "Setlocal enabledelayedexpansion" doesn't change anything in this litte batch file so I dropped it.

Summary: this works for me:

@echo off
for /f "eol=! tokens=3*" %%i in ('reg query "HKEY_CURRENT_USER\Software\Conceptworld\Piky\Settings" /v "installpath"') do echo %%i %%j
Sign up to request clarification or add additional context in comments.

Comments

1

This should work - put a tab character in where it says TAB and leave the space too.

@echo off
for /f "tokens=2,* delims=:TAB " %%i in ('reg query "HKEY_CURRENT_USER\Software\Stupid\MS" /v "installpath"') do ( 
    set "var=%%j"
) 
echo "%var%"
pause

1 Comment

@iask I had used the %%i metavariable instead of %%j. The code above is edited.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.