0

I need to read default value of registry from a batch script. The name of certain item's contains some spaces. Also I want to execute for loop in batch file for one two times.

rem @echo OFF

setlocal ENABLEEXTENSIONS
set KEY_NAME="HKEY_CURRENT_USER\Software\abc\xyz pqr"

FOR /F "tokens=1-3 delims=<TAB>" %%A IN ('REG QUERY %KEY_NAME% 2^>nul') DO (
    set ValueName=%%A
    set ValueType=%%B
    set ValueValue=%%C
)
if defined ValueName (
    @echo Value Name = %ValueName%
    @echo Value Type = %ValueType%
    @echo Value Value = %ValueValue%
) else (
    @echo %KEY_NAME%\%VALUE_NAME% not found.
)
pause

It Gives following output

rem @echo OFF

setlocal ENABLEEXTENSIONS

set KEY_NAME="HKEY_CURRENT_USER\Software\abc\xyz pqr"

FOR /F "tokens=1-3 delims=<TAB>" %A IN ('REG QUERY "HKEY_CURRENT_USER\Software\abc\xyz pqr" 2>nul') DO (
set ValueName=%A
 set ValueType=%B
 set ValueValue=%C
)

(
set ValueName=HKEY_CURREN
 set ValueType=_USER\Software\abc\xyz pq
 set ValueValue=
)

(
set ValueName=    (Default)    REG_SZ    C:\Program Files (x86)\abc\
 set ValueType=
 set ValueValue=
)

(
set ValueName=
 set ValueType=uthor    REG_SZ    gj
 set ValueValue=
)

(
set ValueName=    Version    REG_SZ    1.4.0.0
 set ValueType=
 set ValueValue=
)

if defined ValueName (



)  else ()
Value Name =     Version    REG_SZ    1.4.0.0
Value Type =
Value Value =

pause
Press any key to continue . . .

I would like to get following output and also want that this for loop should stop once I get value of (Default)

(
set ValueName=(Default)
set ValueType=REG_SZ
set ValueValue=C:\Program Files (x86)\abc\
)

Thanks in advance for helping me!

Vinkesh Shah
  • 127
  • 1
  • 1
  • 11
  • You need [delayed expansion](http://ss64.com/nt/delayedexpansion.html). To use _space_ and _tab_ as delimiters, simply omit the `delims` option as they are the default anyway; `` specifies to use those five characters literally... – aschipfl Sep 09 '16 at 10:40

1 Answers1

0

This commented batch code should work for this task on Windows XP and later Windows versions.

@echo off
rem On Windows Vista and later REG.EXE outputs without version info:

rem HKEY_CURRENT_USER\Software\abc\xyz pqr
rem    (Default)    REG_SZ    C:\Program Files (x86)\abc\

rem There are only spaces used to separate value name, value type and value string.


rem But REG.EXE version 3.0 outputs on Windows XP with version info:

rem ! REG.EXE VERSION 3.0
rem
rem HKEY_CURRENT_USER\Software\abc\xyz pqr
rem     <NO NAME>   REG_SZ  C:\Program Files (x86)\abc\

rem NOTE: There are 4 indent spaces and 2 separating tabs in REG 3.0 output line.


rem So either token 2 or token 3 contains value type REG_SZ
rem used to identify the line with the wanted information.
set "TypeToken=2"

:GetPathFromRegistry
for /F "skip=1 tokens=%TypeToken%*" %%A in ('%SystemRoot%\System32\reg.exe QUERY "HKCU\Software\abc\xyz pqr" /ve 2^>nul') do (
    if "%%A" == "REG_SZ" (
        set "AppPath=%%~B"
        goto HaveAppPath
    ) else if "%%A" == "NAME>" (
        set "TypeToken=3"
        goto GetPathFromRegistry
    )
)

echo Failed to read application path from registry.
pause
goto :EOF

:HaveAppPath
echo Application path is: %AppPath%
pause

On Windows VISTA and later Windows versions the FOR loop processes only 1 line from output of REG.

On Windows XP and Windows Server 2003, the FOR loop is run twice with different values for tokens=X* and processes more lines before getting the string value of interest because of the version information output by reg.exe version 3.0.

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 /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • reg /?
  • reg query /?
  • set /?
Mofi
  • 46,139
  • 17
  • 80
  • 143