Solved

Batch file - Convert registry value to variable

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

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

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 (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). 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 http://bit.ly/XDcourse.

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