Reading a Value Data from Registry key list

Hi here is my scenario,
I need to return the Data from Registry Key  and export in to html report by getting Input from txt file with Reg Key and Reg Value separate by commas.
I manage get script read and export to html (Script1), but need to modify the Script2 to read from file which I tried but missing out something. I am not getting Value Data for each Reg Key in the reginput.txt

Here is my script:
Script1: 
FOR /F "skip=2 tokens=2,*" %%A IN ('reg query "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa" /v "restrictanonymous"') DO set "Output=%%B"
ECHO Result = %Output%

Script2:
@echo off & setlocal enabledelayedexpansion
for /F "tokens=1-2 delims=," %%A in (.\reginput.txt) do (
Set _var1=%%A
  Set _var2=%%B
echo !_var1!
echo !_var2!
(
FOR /F "skip=2 tokens=2,*" %%i IN ('reg query !_var1! /v !_var2!') DO set /a "OPT=%%B"
))
ECHO Output = %OPT%

Open in new window

Attached the Text file having the Reg Key and Reg Value
reginput.txt
nssasikumarAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
Give this a try:

@echo off
setlocal EnableDelayedExpansion

for /F "tokens=1-2 delims=," %%A in (.\reginput.txt) do (
  set key=%%A
  set val=%%B
  for /f "skip=2 tokens=2,*" %%i in ('reg query !key! /v !val!') do (
    set /a "OPT=%%j"
    echo Output = !OPT!
  )
)

Open in new window

Which can even be condensed to:
@echo off
setlocal EnableDelayedExpansion

for /F "tokens=1-2 delims=," %%A in (.\reginput.txt) do (
  for /f "skip=2 tokens=2,*" %%i in ('reg query %%A /v %%B') do (
    set /a "OPT=%%j"
    echo Output = !OPT!
  )
)

Open in new window

~bp
nssasikumarAuthor Commented:
Hi Bill,
Thanks for correcting the script, I got the output for Numeric value, but I need output as follows:
1. Need to return string values
2. If RegKey or RegValue is not avail it should return msg as not avail
3. Also need to assign the return values to a variable and printout
eg: the value for Regkey1 is 1
      The value for Regkey2 is 0
Do you want me to raise separate question?, Need help on the same
Bill PrewIT / Software Engineering ConsultantCommented:
Can you explain a bit more about how this code will be used?  That would help me create a solution that better meets your needs.

In addition, a few questions on your feedback:

1. Need to return string values

What other types of registry data do you need to handle, I went by the actual values you provided in your post, which were both numeric.  The following data types exist in registry values, which ones are you needing?

https://msdn.microsoft.com/en-us/library/windows/desktop/ms724884%28v=vs.85%29.aspx

2. If RegKey or RegValue is not avail it should return msg as not avail

Is this message just written to the console?

3. Also need to assign the return values to a variable and printout
eg: the value for Regkey1 is 1
      The value for Regkey2 is 0


What names should be used for each BAT variable created?  If they are just a sequential ordered numbering (like Var1, Var2, Var3, ...) how will you know what they mean later in the BAT script when you need to reference them?

Again, if you describe the larger picture of how you intend to use this it will save time and get us to a better solution I think.

~bp
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Bill PrewIT / Software Engineering ConsultantCommented:
Just to give you some additional ideas on what we could do, here is another version that reports missing reg entries, and will handle strings.  It also places the found values into environment variables var1, var2, var3, ...

@echo off
setlocal EnableDelayedExpansion

set i=1
for /F "tokens=1-2 delims=," %%A in (.\reginput.txt) do (
  set var!i!=
  for /f "tokens=2*" %%i in ('reg query "%%~A" /v "%%~B" 2^>nul ^|findstr /I /L /C:"REG_"') do (
    set "var!i!=%%j"
  )
  if defined var!i! (
    set /a i+=1
  ) else (
    echo.Registry entry not found for:
    echo.  KEY:%%~A
    echo.  VALUE:%%~B
  )
)

set var

Open in new window

~bp
nssasikumarAuthor Commented:
Hi Bill,

1) I need to generate HTML report by returning the Data from Registry  Value (REG_SZ, REG_DWORD).
2) If the Registry Value not avail it should given me MSG "Reg Value not avail", If avail it should return the Data
3) Can use any variable name, I need the output in HTML like:
HKLM\System\CurrentControlSet\Services\eventlog\Application,MaxSize :  20971520
HKLM\System\CurrentControlSet\Services\eventlog\Application,DisplayNameFile : %systemroot%\system32\wevtapi.dll
Bill PrewIT / Software Engineering ConsultantCommented:
Give this a try:

@echo off
setlocal EnableDelayedExpansion

rem Define location for input and output files
set InputFile=reginput.txt
set OutputFile=regoutput.txt

rem Delete output file if it exists
if exist "%OutputFile%" del "%OutputFile%"

rem Process each line of the input file
for /F "usebackq tokens=1-2 delims=," %%A in ("%InputFile%") do (
  set data=

  rem Split appart the reg key and reg value from input file (comma delim), and query the registry for that value
  for /f "tokens=*" %%Z in ('reg query "%%~A" /v "%%~B" 2^>nul ^|findstr /i /l /c:"REG_"') do (

    rem Remove reg value name from result
    set query=%%Z
    set query=!query:%%~B=!

    rem Parse result to get the data type and data content
    for /f "tokens=1*" %%a in ("!query!") do (

      rem If this is a numeric value conver from hex notation
      set "data=%%~b"
      if "!data:~0,2!" == "0x" set /a "data=%%b"

    )

  )

  rem If We got the data, write it out to output file, otherwise display error that not found
  if defined data (
    echo.%%~A,%%~B: !data!>>"%OutputFile%"
  ) else (
    echo.*ERROR* Registry entry not found for:
    echo.  KEY:%%~A
    echo.  VALUE:%%~B
  )
)

Open in new window

~bp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.