Jump to content

Delete Registry Value From File?


Recommended Posts

Hi I'm trying to get my syntax right, can anyone please offer a suggestion?

This is in my text file Def.txt:

HKEY_CLASSES_ROOT\JavaPlugin.FamilyVersionSupport

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders,'C:\Program Files (x86)\Java\'

and this is my code...how do I either format my RegDelete or the text file so I can delete both values?

DefReadDelete()

Func DefReadDelete()
    Dim $Defs
    If Not _FileReadToArray("Def.txt", $Defs) Then
        MsgBox(4096, "Error", " Error reading Array    error:" & @error)
        Exit
    Else
        For $i = 1 To $Defs[0]
            MsgBox("", "Readit", $Defs[$i])
            Local $KeyCheck = RegRead($Defs[$i], "")
            If @error == -1 Then
                MsgBox("", "Would Have Been Deleted", $Defs[$i])
                ;RegDelete($Defs[$i])
            EndIf
        Next
    EndIf
EndFunc   ;==>DefReadDelete

I need to be able to delete Registry Keys, Values and Data read from the Defs.txt file. "C:\Program Files (x86)\Java\" is a string value.

Thanks any suggestions,

-Mike

Link to comment
Share on other sites

According to RegDelete function documentation:

RegDelete

--------------------------------------------------------------------------------

Deletes a key or value from the registry.

RegDelete ( "keyname" [, "valuename"] )

Parameters

keyname The registry key to delete.

valuename [optional] The valuename to delete.

The function expects at least one parameter (keyname). If you look in regedit you can see "folder" icons - those are the the "keynames". Inside them there are values ("valuename") and you can see these on the right-hand tab of regedit.

If you call RegDelete with only one parameter (keyname) it will delete that key (and all values inside); If you specify a "valuename" in addition to the "keyname" then it will delete only that value.

Be warned that modifying/deleting keys from registry can be dangerous - always create a backup before doing this.

You can create a key in the registry under HKEY_CURRENT_USER, some values and have a play with Regdelete on these. Once you are familiar with the procedure - move to the "Real thing".

EDIT:

To answer your specific question:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders,'C:\Program Files (x86)\Java\'

It looks like the entries in your file are comma separated - in the case above, the "keyname" is the first part: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders and the "valuename" is the second part, without the enclosing quotes: C:\Program Files (x86)\Java\

If there is no comma in the line: HKEY_CLASSES_ROOT\JavaPlugin.FamilyVersionSupport that could mean the whole key.

I cannot be sure 100% because it is not my computer, not my software and not my files - I am only giving you the fork - if you chose to eat or not, that is up to you.

Edited by enaiman

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

EDIT:

To answer your specific question:

It looks like the entries in your file are comma separated - in the case above, the "keyname" is the first part: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders and the "valuename" is the second part, without the enclosing quotes: C:\Program Files (x86)\Java\

If there is no comma in the line: HKEY_CLASSES_ROOT\JavaPlugin.FamilyVersionSupport that could mean the whole key.

Thanks for the fork enaiman...good information. Sadly I'm still not able to delete both lines from my registry. My Defs.txt file now looks like this:

HKEY_CLASSES_ROOT\JavaPlugin.FamilyVersionSupport
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders,'C:\Program Files (x86)\Java\'

The first entry is a key and it get's deleted just fine, it's the second line that is giving me fits. Something about how I have my RegDelete statement or how it's formatted in the Def.txt file is preventing it from being deleted. In my code example above, I never get a MsgBox stating that line two Would Have Been Deleted...basically I have the syntax wrong, I'm assuming. I've tried the following in my Def's file and nothing deletes that valuename (there is no 'value data').

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders,C:\Program Files (x86)\Java\
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders,'C:\Program Files (x86)\Java\'
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders'","'C:\Program Files (x86)\Java\'
"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders,'C:\Program Files (x86)\Java\'"

Something is still not as it needs to be and I'm floundering at a solution... :unsure:

Thanks for the reply,

-Mike

P.S. Here is the .reg file for the valuname I'm trying to delete.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders]
"C:\\Program Files (x86)\\Java\\"=""
Edited by mdwerne
Link to comment
Share on other sites

RegDelete($KeyName)

RegDelete($KeyName, $ValueName); <-- the comma cannot be part of the string.

script needs 2 RegDelete statements - one for keys and the other for valuenames.

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

Looks like my RegRead is where I had a syntax issue...not sure what it was, but this seems to be doing the trick.

Func DefReadDelete()
    Dim $i, $RegDefs, $Result
    DirCreate(@TempDir & "\JavaUninstall")
    FileInstall("RegDefs.dat", @TempDir & "\JavaUninstall\RegDefs.dat", 1)
    If Not _FileReadToArray(@TempDir & "\JavaUninstall\RegDefs.dat", $RegDefs) Then
        _FileWriteLog(@HomeDrive & "\JavaUninstall.log", " Error reading defintions" & $RegDefs[$i])
    Else
        For $i = 1 To $RegDefs[0]
            $Result = RegDelete($RegDefs[$i])
            If $Result == 1 Then
                _FileWriteLog(@HomeDrive & "\JavaUninstall.log", " Registry entry deleted: " & $RegDefs[$i])
            EndIf
        Next
    EndIf
EndFunc

Thanks again for the fork enaiman!!!

-Mike

Link to comment
Share on other sites

RegDelete($KeyName)

RegDelete($KeyName, $ValueName); <-- the comma cannot be part of the string.

script needs 2 RegDelete statements - one for keys and the other for valuenames.

Thanks ripdad!!

Makes sense...I will break my Defs file into two sections.

-Mike

Edited by mdwerne
Link to comment
Share on other sites

@ripdad

Ok...so what your saying makes sense, but I'm unclear how I correctly read from my RegVals.dat file (see below) into a array that I can run against RegDelete().

Here are a couple lines from RegVals.dat:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders,C:\Program Files (x86)\Java\
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders,C:\Program Files\Java\jre1.3.0_01\

How do I read before the comma into $KeyName and after the comma into $ValueName?

The comma could be changed into any character if needed.

Thanks,

-Mike

Edited by mdwerne
Link to comment
Share on other sites

Okay, heres an example that you should be able to easily translate to your For loop:

Local $array, $string

; this would be one line in your def file
$string = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders|C:\Program Files (x86)\Java\'

If StringInStr($string, '|') Then
    $array = StringSplit($string, '|')
    RegDelete($array[1], $array[2])
Else
    RegDelete($string)
EndIf

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

Okay, heres an example that you should be able to easily translate to your For loop:

Easily...that's funny, none of this comes easily to me. I'll give it a whack and post my best effort.

Thanks again,

-Mike

Link to comment
Share on other sites

maybe something like this ...

Func DefReadDelete()
    Local $array, $RegDefs, $folder = @TempDir & "\JavaUninstall"
    Local $file = @TempDir & "\JavaUninstall\RegDefs.dat"
    Local $log = @HomeDrive & "\JavaUninstall.log"
    ;
    If Not DirCreate($folder) Then Return SetError(-1)
    FileInstall("RegDefs.dat", $file, 1)
    If Not _FileReadToArray($file, $RegDefs) Then
        _FileWriteLog($log, "Error reading defintions")
        Return SetError(-2)
    EndIf
    ;
    For $i = 1 To $RegDefs[0]
        If StringInStr($RegDefs[$i], '|') Then

            $array = StringSplit($RegDefs[$i], '|')
            ;split string at '|' or whatever you prefer to use

            $Result = RegDelete($array[1], $array[2]); just delete valuename at key
            ;$array[1] = key
            ;$array[2] = valuename

        Else
            $Result = RegDelete($RegDefs[$i]); delete whole key (all valuenames included)
        EndIf
        ;
        If $Result = 1 Then
            _FileWriteLog($log, " Registry entry deleted: " & $RegDefs[$i])
        Else
            _FileWriteLog($log, " Registry entry not deleted: " & $RegDefs[$i])
        EndIf
    Next
EndFunc

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

I am pretty sure that it's not possible to have a comma in either a key name or value name so you can safely split the line for comma.

I have modified rpdad's function - just a "makeup" and it will use your original file format - the functionality is still the same. It will use comma as separator and it will strip the quotes before and after the key value.

Func DefReadDelete()
    Local $array, $RegDefs, $folder = @TempDir & "\JavaUninstall"
    Local $file = @TempDir & "\JavaUninstall\RegDefs.dat"
    Local $log = @HomeDrive & "\JavaUninstall.log"
    Local $valName = ""
    ;
    If Not DirCreate($folder) Then Return SetError(-1)
    FileInstall("RegDefs.dat", $file, 1)
    If Not _FileReadToArray($file, $RegDefs) Then
        _FileWriteLog($log, "Error reading defintions")
        Return SetError(-2)
    EndIf
    ;
    For $i = 1 To $RegDefs[0]
        $array = StringSplit($RegDefs[$i], ',')
        Switch $array[0]
            Case 1          ;no comma
                $Result = RegDelete($array[1]); just delete valuename at key
            Case 2          ;one comma
                $valName = StringStripWS($array[2], 3)
                If StringLeft($valName, 1) = "'" Then $valName = StringTrimLeft($valName, 1)
                If StringRight($valName, 1) = "'" Then $valName = StringTrimRight($valName, 1)
                $Result = RegDelete($array[1], $valName)
            Case Else       ;more than one or other situation - > error
                $Result = 0
        EndSwitch
        If $Result = 1 Then
            _FileWriteLog($log, " Registry entry deleted: " & $RegDefs[$i])
        Else
            _FileWriteLog($log, " Registry entry not deleted: " & $RegDefs[$i])
        EndIf
    Next
EndFunc

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Thank you both for your alternate methods...it sure helps in understanding all this a bit better.

@ripdad...there is almost no way I would have come to these solutions on my own. The route I had started was very different and, most importantly, not working.

Because I've already merged my two defs files back into one and have already set the '|' delimiter for my testing, I plan to continue my development using ripdads function. As a side note, the RegDefs.dat file is up to almost 2900 lines..hopefully I won't run into any array or AutoIt! limitations?

Thanks again to you both! I owe you each a cold one if you're ever in NM, USA.

-Mike

Link to comment
Share on other sites

maybe something like this ...

Based on the example above...I'm attempting to add an "analyze only" mode that will read if the Key or ValueName exists...and if they do, then write to the log file. Below is my attempt at this but I'm having syntax issues with RegRead. Any thoughts?

Func DefReadDelete()
    Local $array, $RegDefs, $folder = @TempDir & "\JavaUninstall"
    Local $file = @TempDir & "\JavaUninstall\RegDefs.dat"
    Local $log = @HomeDrive & "\JavaUninstall.log"
    ;
    If Not DirCreate($folder) Then Return SetError(-1)
    FileInstall("RegDefs.dat", $file, 1)
    If Not _FileReadToArray($file, $RegDefs) Then
        _FileWriteLog($log, "Error reading defintions")
        Return SetError(-2)
    EndIf
    ;
    If $AnalyzeOnly = "UNCHECKED" Then
        For $i = 1 To $RegDefs[0]
            If StringInStr($RegDefs[$i], '|') Then

                $array = StringSplit($RegDefs[$i], '|')
                ;split string at '|' or whatever you prefer to use

                $Result = RegDelete($array[1], $array[2]); just delete valuename at key
                ;$array[1] = key
                ;$array[2] = valuename

            Else
                $Result = RegDelete($RegDefs[$i]); delete whole key (all valuenames included)
            EndIf
            ;
            If $Result = 1 Then
                _FileWriteLog($log, " Registry entry deleted: " & $RegDefs[$i])
            EndIf
        Next
    ElseIf $AnalyzeOnly = "CHECKED" Then
        For $i = 1 To $RegDefs[0]
            If StringInStr($RegDefs[$i], '|') Then

                $array = StringSplit($RegDefs[$i], '|')
                ;split string at '|' or whatever you prefer to use

                $Result = RegRead($array[1], $array[2]); just read valuename at key
                ;$array[1] = key
                ;$array[2] = valuename

            Else
                $Result = RegRead($RegDefs[$i], ""); read whole key (all valuenames included)
            EndIf
            ;
            If $Result = $RegDefs[$i] Then
                _FileWriteLog($log, " Registry entry would have been deleted: " & $RegDefs[$i])
            EndIf
        Next
    EndIf
EndFunc   ;==>DefReadDelete

Thanks,

-Mike

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...