Batch file - Convert registry value to variable

Posted on 2003-11-14
Last Modified: 2011-10-03
I have the following to extract from the registry, all the contents of "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion".
I then extract the Build number, Service Pack Version and Product Version for Win2000 into separate text files:


Code as follows:

rem --------------------------

Start /Wait Regedit /E c:\tmp.bxt "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion"

Find "CurrentBuildNumber" < c:\tmp.bxt > c:\tmp.cxt
Find "CSDVersion" < c:\tmp.bxt >> c:\tmp.dxt
Find "ProductName" < c:\tmp.bxt >> c:\tmp.ext

rem --------------------------

how on earth would I get it to convert each of the values returned to a variable?

I have tried with
Find "CurrentBuildNumber" < c:\tmp.bxt > %var%
Find "CurrentBuildNumber" < c:\tmp.bxt > c:\tmp.bat
Find "CurrentBuildNumber" < c:\tmp.bxt >> c:\tmpb.bat
set var=c:\tmp.bxt
set var="c:\tmp.bxt"
and MANY MANY different ways!

I would be very grateful if someone would help.

The value returned by:

Find "CurrentBuildNumber" < c:\tmp.bxt
type c:\tmp.ext

is (and is in the same format for the other items): "CurrentBuildNumber"="2195"

More info:

The batch file will run when I log into anyones machine on the network and will eventually check to see what the OS version is and update the service pack if it is lower than a defined value. Also will preform various updates etc.

Thanks for any help :)



I have also tried using as a starting point, but again, it doesn't seem to like it!!
:Returns the correct value into the file Tmp.bat
    Find "=" < %TEMP%.\Tmp.bxt > %TEMP%.\Tmp.bat
:not got a SET command, therefore doesn't run
Question by:Aubs

Author Comment

ID: 9749133
ok, I got the first bit, by doing:

For /F "delims=" %%x in (c:\tmp.cxt) do (
 set str=%%x
 echo %str%)

it echo's the part I want ("CurrentBuildNumber"="2195")

Now then, to remove the " 's

Any help, again greatfully received.

LVL 67

Expert Comment

ID: 9749685
First of all, can you pull the same info (if all you're looking for is the version) from a simple VER command? (or older OSs VER /R)

How is the data being saved in your text files?  Is it saved with the ""s? (can you paste a portion of the file here?)
I believe FOR %~I will remove surrounding quote marks.
LVL 83

Accepted Solution

oBdA earned 250 total points
ID: 9749809
It would work with regedit as well, but there's an easier way, without temporary files, using reg.exe from the W2k Support Tools.
This reads the keys you want, sets environment variables according to the values, and sets the variable "SPLevel" to the numeric value.

@echo off
set Key=HKLM\Software\Microsoft\Windows NT\CurrentVersion
set Value1=CurrentBuildNumber
set Value2=CSDVersion
set Value3=ProductName

for /f "skip=4 tokens=2*" %%a in ('reg query "%Key%" /v "%Value1%"') do set %Value1%=%%b
for /f "skip=4 tokens=2*" %%a in ('reg query "%Key%" /v "%Value2%"') do set %Value2%=%%b
for /f "skip=4 tokens=2*" %%a in ('reg query "%Key%" /v "%Value3%"') do set %Value3%=%%b
set SPLevel=0
if not "%CSDVersion%"=="" for /f "tokens=3" %%a in ("%CSDVersion%") do set SPLevel=%%a

echo Build Number: [%CurrentBuildNumber%]
echo Service Pack: [%CSDVersion%]
echo Product Name: [%ProductName%]
echo SP Level:     [%SPLevel%]

If you have the Support Tools already installed somewhere, just use the reg.exe found there. If you haven't installed them yet, do *not* install them from the CD; go to page below and download the support tools that matches the SP level on the machine you want to install it on (some of the tools get updated with the service packs as well ...).

Windows 2000 Service Packs

Expert Comment

ID: 10019485
I have tried some variations of what has been posted here but it is not working for what I am trying to do.  

Basically I want to extract data from a specific software key and have this data set as a variable so that I can save a file using %variable%.xls within a batch file.

I am trying to inventory all my computers using a batch file I created.  This batch file silently installs a program that takes all inventory of a PC and then saves it to a mapped drive on the network.  

The file format needs to be excel to work with my access database but I would like the name of the file to be from a software value (%variable%) in the registry.

If anyone can assist me with this I would be very grateful.

Thank you


How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

LVL 83

Expert Comment

ID: 10140678
you need to ask a separate question for that ( To help you here wouldn't be fair neither to Aubs, who provided the points, nor to other experts who would/could answer your question, but don't search for open questions that are two months old (and therefore, obviously, asking a new question increases your chances of success because more people will see it).

is your problem solved or do you still need assistance?

Author Comment

ID: 10148663

Well, I don't really know!!!

My target has been reached, albeit in a different way!!

However, I would offer the points to the user who has helped the most.

Unless anyone is in disagreement, I will offer the points to you, oBdA, for your indepth attempt to assist.

Thanks for your help :)



Author Comment

ID: 10148669
hope thay are appreciated ;o)
LVL 83

Expert Comment

ID: 10150434
Sure they're appreciated; out of curiosity: how did you solve it then? A variation of the regedit version using "for /f", kix32, another third party tool?

Author Comment

ID: 10153334
Well, it is quite messy, but this is how I did it:

@echo off
IF %USERNAME%'==BeesleA' (echo %COMPUTERNAME% - Aubs Accessed.&echo %COMPUTERNAME% - Aubs Accessed >> \\server\bs\IT\2003.11.13\log.txt.&Goto A)
IF %USERNAME%'==jcavell' (echo %COMPUTERNAME% - JC Accessed.&echo %COMPUTERNAME% - JC Accessed >> \\server\bs\IT\2003.11.13\log.txt.&Goto A)
IF %USERNAME%'==Administrator' (echo %COMPUTERNAME% - Admin Accessed.&echo %COMPUTERNAME% - Admin Accessed >> \\server\bs\IT\2003.11.13\log.txt.&Goto A)
goto DoneEnd
IF %SystemRoot%'==C:\WINNT' (echo %COMPUTERNAME% - Windows 2000.&echo %COMPUTERNAME% - Windows 2000 >> \\server\bs\IT\2003.11.13\log.txt.&Goto 2k)
IF %SystemRoot%'==C:\WINDOWS' (echo %COMPUTERNAME% - Windows XP.&echo %COMPUTERNAME% - Windows XP >> \\server\bs\IT\2003.11.13\log.txt.&Goto XP)
goto DoneEnd

call \\server\bs\IT\2003.11.13\W2000.exe
echo %COMPUTERNAME% - Completed Windows 2000 update
echo %COMPUTERNAME% - Completed Windows 2000 update >> \\server\bs\IT\2003.11.13\log.txt
goto OSEnd

call \\server\bs\IT\2003.11.13\WXP.exe
echo %COMPUTERNAME% - Completed Windows XP update
echo %COMPUTERNAME% - Completed Windows XP update >> \\server\bs\IT\2003.11.13\log.txt
echo. >> \\server\bs\IT\2003.11.13\log.txt
goto OSEnd


echo Please Select:
echo 1 - Internet Explorer 5.5
echo 2 - Internet Explorer 6.0
echo 3 - Internet Explorer 6.0SP1

set /P CH=
if /I "%CH%"=="1" goto 5.5
if /I "%CH%"=="2" goto 6.0
if /I "%CH%"=="2" goto 6.0SP1
goto DoneEnd

call \\server\bs\IT\2003.11.13\IE5.5sp2.exe
echo Completed IE5.5 SP2 update
echo %COMPUTERNAME% - Completed IE5.5 SP2 update >> \\server\bs\IT\2003.11.13\log.txt
echo %COMPUTERNAME% >> \\server\bs\IT\2003.11.13\log.txt
goto DoneEnd

call \\server\bs\IT\2003.11.13\IE6.0.exe
echo Completed IE6.0 update
echo %COMPUTERNAME% - Completed IE6.0 update  >> \\server\bs\IT\2003.11.13\log.txt
echo %COMPUTERNAME% >> \\server\bs\IT\2003.11.13\log.txt
goto DoneEnd

call \\server\bs\IT\2003.11.13\IE6.0SP1.exe
echo Completed IE6.0SP1 update
echo %COMPUTERNAME% - Completed IE6.0SP1 update  >> \\server\bs\IT\2003.11.13\log.txt
echo %COMPUTERNAME% >> \\server\bs\IT\2003.11.13\log.txt
goto DoneEnd


Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

I don't know if many of you have made the great mistake of using the Cisco Thin Client model with the management software VXC. If you have then you are probably more then familiar with the incredibly clunky interface, the numerous work arounds, and …
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension ( This reminded me of questions tha…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now