Solved

Batch file - Convert registry value to variable

Posted on 2003-11-14
9
2,603 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 85

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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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
 
LVL 85

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 85

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

Technology Partners: 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!

Question has a verified solution.

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

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…
I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Suggested Courses

615 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