Jump to content

delete registrykey in a .reg-file


Recommended Posts

Hi guys,

I have a registryfile called start.reg,

[HKEY_LOCAL_MACHINE\SOFTWARE\MMM\Prog1]
"aaa"="aaa"

[HKEY_LOCAL_MACHINE\SOFTWARE\MMM\Prog2]
"aaa"="bbb"

[HKEY_LOCAL_MACHINE\SOFTWARE\MMM\Prog3]
"aaa"="ccc"

now I want Autoit to delete the complete key "HKEY_LOCAL_MACHINE\SOFTWARE\MMM\Prog3" and "HKEY_LOCAL_MACHINE\SOFTWARE\MMM\Prog2" other some more, with oneclick and save it.

I need a simple script, where I just need to write the keys I want to delete in it.

In words: Autoit opens the start.reg, deletes the specified keys and saves the file.

My second question is, how can I "clean" a registry-file?

i.e. the reg-file's content is:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE]
[HKEY_LOCAL_MACHINE\SOFTWARE]
[HKEY_LOCAL_MACHINE\SOFTWARE\MMM]
[HKEY_LOCAL_MACHINE\SOFTWARE\MMM\Prog3]
"aaa"="ccc"

The first three lines doesnt make sense, so I want Autoit to make it to

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\MMM\Prog3]
"aaa"="ccc"

How can I do this? I'm beginner.

Thx

Edited by svatvn
Link to comment
Share on other sites

because I don't want to edit the local registry.

I want to edit an existing .reg-file...

The reg-files were created from a registry-snapshot tool, which shows me the changes, which was made by some program, but the reg-file also contains some keys, whichs I want to delete automatically...

I already have to code for find and replace a key in the registry file.

$szFile = "start.reg"
$szText = FileRead($szFile,FileGetSize($szFile))


$szText = StringReplace($szText, "[HKEY_LOCAL_MACHINE\SOFTWARE\MMM\ProgOld]", "[HKEY_LOCAL_MACHINE\SOFTWARE\MMM\ProgNew]")


FileDelete($szFile)
FileWrite($szFile,$szText)

But now I want to delete the whole key...

Edited by svatvn
Link to comment
Share on other sites

First off. DON't clean that file. Those first three lines make perfect sense to Windows.

To solve your problem

$sStr = FileRead($szFile)
$sStr = StringRegExpReplace($sStr, "(?i)(?m:^)(\[)(.+prog[23]\]", "$1-$2")
$hFile = FileOpen($szFile, 2)
FileWrite($hFile, $sStr)
FileClose($hFile)

That should leave you with a file that looks like

[HKEY_LOCAL_MACHINE\SOFTWARE\MMM\Prog1]

"aaa"="aaa"

[-HKEY_LOCAL_MACHINE\SOFTWARE\MMM\Prog2]

"aaa"="bbb"

[-HKEY_LOCAL_MACHINE\SOFTWARE\MMM\Prog3]

"aaa"="ccc"

EDIT:

I just remembered something.

You may also have to empty the values for those 2 keys so the entries would look like.

"aaa"=""

If that's the case then come back and we will get that part done as well.

Also I'm pretty sure that the keys you mention are not valid so the Expression above will not work. You can however use it for testing and to see the principal involved. All hou have to do is add in the minus sign for the keys that should be deleted.

EDIT 2:

I knew if I looked I would have the link to that information someplace.

http://support.microsoft.com/kb/310516

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

oh, I'm totally confused and disorientated...

Let me describe it again:

I have a registry-file: start.reg

the content is:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion]
"SyncTime"=hex:00,00,00,00,00,00,00,00
"Active"=dword:00000000

[HKEY_CURRENT_USER\Software\Classes\Local Settings\MuiCache]
"Active"=dword:00000000

[HKEY_LOCAL_MACHINE\SOFTWARE\MMM\Prog1]
"aaa"="aaa"

now I want Autoit to delete the keys:

HKEY_CURRENT_USER\Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion

HKEY_CURRENT_USER\Software\Classes\Local Settings\MuiCache

and makes the file start.reg to this:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\MMM\Prog1]
"aaa"="aaa"

so, what AutoIt should do is, to delete this lines

[HKEY_CURRENT_USER\Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion]
"SyncTime"=hex:00,00,00,00,00,00,00,00
"Active"=dword:00000000
[HKEY_CURRENT_USER\Software\Classes\Local Settings\MuiCache]
"Active"=dword:00000000

AutoIt should find all the keys to be deleted in a seperate file called key2del.txt with content:

HKEY_CURRENT_USER\Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion

HKEY_CURRENT_USER\Software\Classes\Local Settings\MuiCache

So I only need to write the keys, which I want to delete in the textfile, and autoit deletes them from the start.reg.

If I would want to delete both keys in the local registry, it would be easy with a registryfile with the content:

Windows Registry Editor Version 5.00

[-HKEY_CURRENT_USER\Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion]
[-HKEY_CURRENT_USER\Software\Classes\Local Settings\MuiCache]

BUT, I dont want to delete it from the registry, I want to delete it from my start.reg file.

The background of the whole thing is, that I trace my registry changes with the software "Registry Workshop".

For that, it compares 2 snapshots of the registry, and can export the changes to a *.reg file

Now, the regfile has a lot of keys, which are just system changes, not really a "change", so I want to delete them from the reg file..

I hope you understand...

Edited by svatvn
Link to comment
Share on other sites

Hi.

;ModMyRegFile
#include <file.au3>
#include <array.au3>

$RegTxt = "Windows Registry Editor Version 5.00" & @CRLF & _
        @CRLF & _
        '[HKEY_CURRENT_USER\Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion]' & @CRLF & _
        '"SyncTime"=hex:00,00,00,00,00,00,00,00' & @CRLF & _
        '"Active"=dword:00000000' & @CRLF & _
        @CRLF & _
        '[HKEY_CURRENT_USER\Software\Classes\Local Settings\MuiCache]' & @CRLF & _
        '"Active"=dword:00000000' & @CRLF & _
        @CRLF & _
        '[HKEY_LOCAL_MACHINE\SOFTWARE\MMM\Prog1]' & @CRLF & _
        '"aaa"="aaa"' & @CRLF

$MyReg = "C:\temp\Test.reg"
$h = FileOpen($MyReg, 2 + 8)
FileWrite($h, $RegTxt)
FileClose($h)



Dim $aRegDel[3] = [2, _ ; this is an array of all the keys you want to clean out from your *.REG files
"[HKEY_CURRENT_USER\Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion]", _
"[HKEY_CURRENT_USER\Software\Classes\Local Settings\MuiCache]"]

_ArrayDisplay($aRegDel)

Dim $aReg
_FileReadToArray($MyReg, $aReg)
_ArrayDisplay($aReg)


for $d = 1 to $aRegDel[0]

Dim $SectStart=False, $SectEnd

for $i = 1 to $aReg[0]
    if $aReg[$i]=$aRegDel[$d] Then
        ConsoleWrite($i & @CRLF)
        $SectStart=$i
    Else
        if $SectStart and StringLeft($aReg[$i],1)= "[" then ; a new "REG Section starts...
            ExitLoop
        Else
            $SectEnd=$i
        EndIf
    EndIf
Next

for $i = $SectEnd to $SectStart Step -1 ; It's safer to delete array elements BACKWARDS, as you don't risk to "shrink" the array smaller than $i...
    _ArrayDelete($aReg,$i)
Next


$aReg[0]=UBound($aReg)-1
_ArrayDisplay($aReg)
Next

regards, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...