Solved

Batch file --- Search registry for a key, Find and replace text in text file

Posted on 2004-04-16
14
2,138 Views
Last Modified: 2007-12-19
Halo,

Been trawling the net but couldn't find the answer I'm looking for.
I'm trying to automate an uninstallation from a batch file.
Basically I'm looking to write a batch file to perform these functions.

1) Search Win XP registry for the key "DisplayName" and compare if the value is
    to what I want. Say I want to find "DisplayName" key with "Paint Shop Pro" value.
    To keep searching for "DisplayName" until found the one with the value "Paint Shop Pro".

2) Once found, to store the FullKey path in a variable. Using this path variable, I can
    easily read the value of the "UninstallString" key to perform my next command.
    I believe the code for reading a registry key's value using REG QUERY is:
    for /f "tokens=3" %%p IN ('reg query FULLKEY-PATH-HERE /v UninstallString') do set value=%%p

3) To edit a text file, search for the string "z:\web\files" and replace it with "c:\web\files"


Is throwing in generous EE points for the expert who can provide me with the working codes
[tried and tested] and just a brief description. Hope to be hearing positive news!
0
Comment
Question by:moccajava
  • 9
  • 5
14 Comments
 
LVL 30

Accepted Solution

by:
SteveGTR earned 500 total points
ID: 10844236
Try this to find the key/value in HKLM:

@echo off
setlocal ENABLEDELAYEDEXPANSION

reg EXPORT HKLM _reg.out /nt4

for /f "tokens=1-2 delims==" %%a in (_reg.out) do (
  set key1=%%a
 
  if "!key1:~0,1!"=="[" (
    set lastKey=!key1:~1,-1!
  ) else (
    if %%a=="DisplayName" (
      if %%b=="Paint Shop Pro" (
            echo Key:    !lastKey!
            echo SubKey: %%a
            echo Value:  %%b
      )
    )
  )
)

if exist _reg.out del _reg.out > NUL

Good Luck,
Steve
0
 

Author Comment

by:moccajava
ID: 10848800
Hi Steve,

I tried your script and it seems fine as it did spew out occurrances of Subkey "DisplayName"
and Value "Paint Shop Pro". However, it took a long while so I changed the export path to be
"reg EXPORT HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall _reg.out /nt4"
to minimise the wait and zero in on the search.

While it did bring me to the correct path successfully, I am not able to save the full path
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{D6DE02C7-blablabla}
to a variable (set variable = !lastKey!"). At this point I do have some questions...

1) Can you help me briefly explain wat it means and wat does it do:
     if "!key1:~0,1!"=="[" (
    set lastKey=!key1:~1,-1!

2) Along with the subkey "DisplayName", there is also the subkey "UninstallString" under the same
    path. My aim is to collect the value of this subkey to run it (MsiExec.exe /I{D6DE02C7-blablabla}
    using the reg query command:

    for /f "tokens=3" %%p IN ('reg query -insert path saved from !lastKey!- /v UninstallString')
    do set variable=%%p
    echo Uninstalling %%b now! & %variable%

It might not be the correct method so please help! If you feel that some alterations to your code
is suffice to immediately read the value of "UninstallString" then all the better.

Thanks and look forward to your input.
0
 

Author Comment

by:moccajava
ID: 10848817
Oh one more info...

The data type of "UninstallString" seems to be REG_EXPAND_SZ.
I'm not really sure bout this registry thingie but hope this little info helps!

Thanks!
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 10849121
Great change to speed it up.

1) Can you help me briefly explain wat it means and wat does it do:
     if "!key1:~0,1!"=="[" (
    set lastKey=!key1:~1,-1!

This line detects when keys are being processed. The processing assumes that any line that begins with "[" is a key. If so then it strips the "[" and "]" (at the end) and saves it in the lastKey field. The process makes use of the delayed expansion processing by using "!" to reference the key1 environment variable. You have to do this within this kind of look or else it won't evaluate correctly. See set /? for more information.

I've modified the batch file:

@echo off
setlocal ENABLEDELAYEDEXPANSION

set product=Paint Shop Pro
set foundKey=
set uninstallKey=

reg EXPORT HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall  _reg.out /nt4

for /f "tokens=1-2 delims==" %%a in (_reg.out) do (
  set key1=%%a
 
  if "!key1:~0,1!"=="[" (
    if "!foundKey!"=="Y" goto FOUNDIT      
    set lastKey=!key1:~1,-1!
  ) else (
    if %%a=="DisplayName" (
      if %%b=="%product%" (
      set foundKey=Y
      )
    ) else (
      if %%a=="UninstallString" set uninstallKey=%%b
    )  
  )
)

REM if exist _reg.out del _reg.out > NUL

goto :EOF

:FOUNDIT

echo Product:  %product%
echo Key:        %lastKey%
echo Uninstall: %uninstallKey%

You can do the extra processing you require in the :FOUNDIT routine.

I hope this helps :)
Steve
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 10849123
Got rid of one set of unnecessary parens:

@echo off
setlocal ENABLEDELAYEDEXPANSION

set product=Norton AntiVirus 2004 (Symantec Corporation)
set foundKey=
set uninstallKey=

reg EXPORT HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall  _reg.out /nt4

for /f "tokens=1-2 delims==" %%a in (_reg.out) do (
  set key1=%%a
 
  if "!key1:~0,1!"=="[" (
    if "!foundKey!"=="Y" goto FOUNDIT      
    set lastKey=!key1:~1,-1!
  ) else (
    if %%a=="DisplayName" (
      if %%b=="%product%" set foundKey=Y
    ) else (
      if %%a=="UninstallString" set uninstallKey=%%b
    )  
  )
)

REM if exist _reg.out del _reg.out > NUL

goto :EOF

:FOUNDIT

echo Product:  %product%
echo Key:        %lastKey%
echo Uninstall: %uninstallKey%
0
 

Author Comment

by:moccajava
ID: 10849238
The "uninstallKey" variable returns a hex code instead of the string due to
the data type being a REG_EXPAND_SZ.

How to convert it to a string?

Good job so far!
Thanx
0
 

Author Comment

by:moccajava
ID: 10849549
However if you cant find a way to convert it to a string then the option to read it
from reg query command is always available but in that case another modification
to your code has to be done.

Just my 2 cents worth...
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 30

Expert Comment

by:SteveGTR
ID: 10849675
That doesn't make any sense that the UninstallKey is in hex... None of the uninstallkey's on my system are like that. All of them look like commands that can be run from the command line. Is there a value for QuietUninstallString?

How about you look in the _reg.out file and display what's in there in a message. Display all the information under the key ([]).

The last post I had remarked out the _reg.out file so you can open it with a text editor:

REM if exist _reg.out del _reg.out > NUL

Here's an example of another program from my _reg.out:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Ad-aware 6 Personal]
"DisplayName"="Ad-aware 6 Personal"
"UninstallString"="C:\\PROGRA~1\\LAVASOFT\\AD-AWA~1\\UNWISE.EXE C:\\PROGRA~1\\LAVASOFT\\AD-AWA~1\\INSTALL.LOG"
"DisplayVersion"="6.0"
"HelpLink"="http://www.lavasoftusa.com"
"Publisher"="Lavasoft Sweden"
"URLInfoAbout"="http://www.lavasoftusa.com"
"Comments"="Ad-aware VI Personal"
0
 

Author Comment

by:moccajava
ID: 10851327
From your first post, I have commented out the code that deletes _reg.out coz I wanna see
what's going on. I'm trying to learn and understand what's going on too. Unfortunately, I had a look
and while yes most other software have their own uninstall command in string, I think PSP uses the microsoft installer (msi) and that is why it's in reg_expand_sz hex.

I'll cut and paste what's returned in the _reg.out:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{D6DE02C7-1F47-11D4-9515-00105AE4B89A}]
"Comments"="Paint Shop Pro 7 ESD"
"DisplayVersion"="7.0.1.0000"
"HelpLink"=hex(2):68,74,74,70,3a,2f,2f,77,77,77,2e,6a,61,73,63,2e,63,6f,6d,00
"ModifyPath"=hex(2):4d,73,69,45,78,65,63,2e,65,78,65,20,2f,49,7b,44,36,44,45,\
  30,32,43,37,2d,31,46,34,37,2d,31,31,44,34,2d,39,35,31,35,2d,30,30,31,30,35,\
  41,45,34,42,38,39,41,7d,00
"NoRepair"=dword:00000001
"Readme"=hex(2):43,3a,5c,50,72,6f,67,72,61,6d,20,46,69,6c,65,73,5c,50,61,69,6e,\
  74,20,53,68,6f,70,20,50,72,6f,20,37,5c,52,65,61,64,6d,65,2e,64,6f,63,00
"Size"=""
"EstimatedSize"=dword:0000a836
"UninstallString"=hex(2):4d,73,69,45,78,65,63,2e,65,78,65,20,2f,49,7b,44,36,44,\
  45,30,32,43,37,2d,31,46,34,37,2d,31,31,44,34,2d,39,35,31,35,2d,30,30,31,30,\
  35,41,45,34,42,38,39,41,7d,00
"VersionMajor"=dword:00000007
"VersionMinor"=dword:00000000
"WindowsInstaller"=dword:00000001
"Version"=dword:07000001
"Language"=dword:00000409
"DisplayName"="Paint Shop Pro 7"
0
 

Author Comment

by:moccajava
ID: 10851384
I played around with the code and successfully got it to work.
However my code might not be efficient so have a look and see if it can be improved on.

... continuation from your code ...
:FOUNDIT
reg query !lastKey! /v UninstallString > dump.txt
for /f "tokens=3" %%p IN (z.txt) do set dump1=%%p     <--- i cant seem to get the whole value coz
for /f "tokens=4" %%q IN (z.txt) do set dump2=%%q    <--- of the "space" delimiter so had to split
echo Product:  %product%
echo Key:        %lastKey%
echo Uninstall: %uninstallKey%
echo.
echo Now uninstalling PSP...
%dump1% %dump2%
0
 

Author Comment

by:moccajava
ID: 10851393
Sorry I've replaced the "z.txt" to "dump.txt" just not reflected on the code here...
0
 

Author Comment

by:moccajava
ID: 10851572
Steve I've somehow solved it thanks.
Lest you feel that no improvements can be made to the code,
then it''s assumed that the problem is solved.

How bout the "find-n-replace" string? Is it possible to do it
using only the given dos commands?

Thanks... 500 pts coming your way pretty soon :p
0
 

Author Comment

by:moccajava
ID: 10851640
Hi Steve,

It's alright. I've managed to solve both questions
with some help from you of course.

For the benefit of others who are interested to know how to do
a "Find-and-Replace" function without the use of a 3rd party tool,
here is the link to copy the code from.

http://www.batch.hpg.ig.com.br/#19a

Steve I will accept your answer and give you the deserved 500 points.
Though I'm still unsure how to cut short the code without needing to
do the FOR loop twice to get the UninstallString value. (see %%dump1 & %%dump2)

Best of luck!
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 10852059
Thanks for the C :(
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

16 Experts available now in Live!

Get 1:1 Help Now