Solved

Batch file - Convert registry value to variable

Posted on 2003-11-14
9
2,593 Views
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:

tmp.cxt
tmp.dxt
tmp.ext

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
a)
Find "CurrentBuildNumber" < c:\tmp.bxt > %var%
b)
Find "CurrentBuildNumber" < c:\tmp.bxt > c:\tmp.bat
c:\tmp.bat
c)
Find "CurrentBuildNumber" < c:\tmp.bxt >> c:\tmpb.bat
c:\tmpb.bat
d)
set var=c:\tmp.bxt
e)
set var="c:\tmp.bxt"
f)
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 :)

Aubs


P.S.

I have also tried using http://www.fpschultze.de/b5.htm 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
    %TEMP%.\Tmp.bat
0
Comment
Question by:Aubs
9 Comments
 

Author Comment

by:Aubs
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.

Aubs
0
 
LVL 67

Expert Comment

by:sirbounty
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.
0
 
LVL 83

Accepted Solution

by:
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.

====8<----[SPLevel.cmd]----
@echo off
setlocal
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%]
====8<----[SPLevel.cmd]----

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
http://www.microsoft.com/windows2000/downloads/servicepacks/
0
 

Expert Comment

by:limester
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

Limester

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 83

Expert Comment

by:oBdA
ID: 10140678
limester,
you need to ask a separate question for that (http://oldlook.experts-exchange.com/help/#2). 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).

Aubs,
is your problem solved or do you still need assistance?
0
 

Author Comment

by:Aubs
ID: 10148663
oBdA,

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 :)

Regards,

Aubs
0
 

Author Comment

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

Expert Comment

by:oBdA
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?
0
 

Author Comment

by:Aubs
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
:A
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

:2k
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

:XP
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.
echo. >> \\server\bs\IT\2003.11.13\log.txt
goto OSEnd


:OSEnd
cls
echo.

echo Please Select:
echo.
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

:5.5
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


:6.0
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

:6.0SP1
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



:DoneEnd
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes a user will call me frantically, explaining that something has gone wrong and they have tried everything (read - they have messed it up more and now need someone to clean up) and it still does no good, can I help them?!  Usually the standa…
Windows 10 is here and for most admins this means frustration and challenges getting that first working Windows 10 image. As in my previous sysprep articles, I've put together a simple help guide to get you through this process. The aim is to achiev…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

932 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

12 Experts available now in Live!

Get 1:1 Help Now