Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2611
  • Last Modified:

Batch file - Convert registry value to variable

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
Aubs
Asked:
Aubs
1 Solution
 
AubsAuthor Commented:
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
 
sirbountyCommented:
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
 
oBdACommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
limesterCommented:
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
 
oBdACommented:
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
 
AubsAuthor Commented:
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
 
AubsAuthor Commented:
hope thay are appreciated ;o)
0
 
oBdACommented:
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
 
AubsAuthor Commented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now