Sign in to follow this  
Followers 0
Virtualburn

Reg Edit / Remove Key / Space in Path

13 posts in this topic

#1 ·  Posted (edited)

Firstly I'd like to say I'm so glad I found this forum and this Program.. I'm sure it's going to great learning how to use this, also after reading through various posts trying to find a solution to this problem it's evident there is a great community here and I hope I can help out in the future.

My issue may be very simple, but as I said I'm new to this. The script was found on the site here : Original Script

Basically I am trying to remove selct registry keys on machines that have 'Canon' or 'Canon ' in the title. This script was perfect and works if there are no spaces in the path. I've tried various combinations of quotations etc but nothing seems to work.

$sKey = 'HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Print\Environments\Windows NT x86\Drivers\Version-3'
$i = 1

While 1
    $Key = RegEnumVal($sKey, $i)
    If @error = -1 Then ExitLoop
    If Not @error And StringRegExp($Key, 'Canon[ ]?') Then
        RegDelete($sKey, $Key)
        ContinueLoop
    EndIf
    $i += 1
WEnd

It's the section 'Windows NT x86' that seems to be causing the issue.

Edited by Virtualburn

~ Make Hay while the Sun shines ~

Share this post


Link to post
Share on other sites



While 1
    $Key = RegEnumVal($sKey, $i)
    If @error Then ExitLoop
    If StringRegExp($Key, "(?i).*\wcanon\w.*) Then
        RegDelete($sKey, $Key)
    EndIf
    $i += 1
WEnd

I changed the expression and removed the continueloop. With continueLoop in there you were never incrementing $i.


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!"

Share this post


Link to post
Share on other sites

Thanks for your response Geo. I seem to getting an AutoIt error now.

Line 7
    If StringRegExp($Key, "(?i).*\wcanon\w.*) Then

Error: Unterminated String

This is the script as it stands.

$sKey = 'HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Print\Environments\Windows NT x86\Drivers\Version-3'
$i = 1

While 1
    $Key = RegEnumVal($sKey, $i)
    If @error Then ExitLoop
    If StringRegExp($Key, "(?i).*\wcanon\w.*) Then
        RegDelete($sKey, $Key)
    EndIf
    $i += 1
WEnd

~ Make Hay while the Sun shines ~

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Are you trying to delete Values or Keys? You say Keys in your example but you are enumerating Values in your script. In my x86 and x64 builds, there are only the default, Directory, MajorVersino, and MinorVersion values stored in that location. In my x86 builds there are Keys for each of the printers that I have (ever) installed. Also, I would change the Regular Expression Search to "(?i)Canon" for a case - insensitive search. There is no reason to include the space in your Regular Expression query...unless you mean to differentiate "Canon v1" from "Canonv1"

EDIT: You're missing double quote at end of StringRegExp query

EDIT EDIT:Actually "(?i)Cannon[ ]?" would match both "Cannon v1" and "Cannonv1"..just caught that

Edited by Varian

Share this post


Link to post
Share on other sites

Just a typo on my end

If StringRegExp($Key, "(?i).*\wcanon\w.*)

should be

If StringRegExp($Key, "(?i).*\wcanon\w.*")


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!"

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Hi Varian, Thanks for your input. Yes they are Keys I am trying to remove. The scenario is that on some machines to solve an issue, all registry Keys in location 'HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Print\Environments\Windows NT x86\Drivers\Version-3' that start with or include 'Canon' need to be removed.

I have ammended the script and it seems to run fine, although the registry keys are still present.

The original code worked fine if the key was here : 'HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Print\Environments'

But using the full path with 'Windows NT x86' included it doesn't. I presumed it was the spaces in the path that were creating the problem.

Edited by Virtualburn

~ Make Hay while the Sun shines ~

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

You need to use RegEnumKey then. Try This:

$sKey = 'HKLM\SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-3'
$i = 1
While 1
    $Key = RegEnumKey($sKey, $i)
    If @error = -1 Then ExitLoop
    If Not @error And StringRegExp($Key, '(?i)Cannon') Then
        RegDelete($sKey, $Key)
    EndIf
    $i += 1
WEnd
If you use RegEnumVal, you will stay in the original Key & look for Values within that key only

Edited by Varian

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

I've tried that and the key is still present. Bit lost as to what to try now.

Edited by Virtualburn

~ Make Hay while the Sun shines ~

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

If you use RegEnumVal, you will stay in the original Key & look for Values within that key only

Just to clarify, it's the whole key that I am trying to remove.

HKLM\SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-3\Canon*

for the moment I have changed 'Canon' in the script to 'Emac' and have created a temp key of the same name to see if this works. As yet, the key is still present.. glaring at me obstinately. :|

Edited by Virtualburn

~ Make Hay while the Sun shines ~

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Test with this

#NoTrayIcon
$sKey = 'HKLM\SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-3'
$i = 1
While 1
    $Key = RegEnumKey($sKey, $i)
    If @error = -1 Then ExitLoop
    If Not @error And StringRegExp($Key, '(?i)Cannon') Then
        $Result = RegDelete($sKey, $Key)
        $Error = @error
        Switch $Result
            Case 1
                _Splash('Success Deleting ' & $sKey & '\' & $Key)
                Sleep(2500)
            Case 0
                _Splash('key/value does not exist')
                Sleep(2500)
            Case 2
                If $Error = 1 Then _Splash('unable to open requested key')
                If $Error = 2 Then _Splash('unable to open requested main key ')
                If $Error = 3 Then _Splash('unable to remote connect to the registry')
                If $Error = -1 Then _Splash('unable to delete requested value')
                If $Error = -2 Then _Splash('unable to delete requested key/value')
                Sleep(2500)
        EndSwitch
    EndIf
    $i += 1
WEnd

Func _Splash($Text) ;Shows a small borderless splash message.
    SplashTextOn('', $Text, @DesktopWidth, 25, -1, 5, 33, '', 14)
EndFunc   ;==>_Splash
Just out of curiosity, you did refresh regedit, right?

Edited by Varian

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

OK, I missed 2 important things

First, your line

RegDelete($sKey, $Key)
is actually trying to delete the VALUE $KEY. It should actually be written as
RegDelete($sKey & '\' & $Key)
which deletes the KEY $KEY.

Secondly, imagine you have 4 Keys in the location: Adobe, Cannon, Cannon BJC, and Lexmark. When $i = 2 you will be at Cannon. When you delete that Key, there will be 3 Keys left: Adobe, Cannon BJC, and Lexmark. If you then increase $i to 3, you will be at Lexmark and miss Cannon BJC. The script should have the ContinueLoop (that you originally had) on a successful deletion. Even if $i is greater than the number of Keys left, the "If @error = -1" line will exit the loop.

This will definitley get you where you need to be!

$sKey = 'HKLM\SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-3'
$i = 1
While 1
    $Key = RegEnumKey($sKey, $i)
    If @error = -1 Then ExitLoop
    If Not @error And StringRegExp($Key, '(?i)Cannon') Then
        _Splash('Working on ' & $sKey & '\' & $Key)
        Sleep(3000)
        $Result = RegDelete($sKey & '\' & $Key)
        $Error = @error
        Switch $Result
            Case 1
                _Splash('Success Deleting ' & $sKey & '\' & $Key)
                Sleep(2500)
                ContinueLoop
            Case 0
                _Splash('key/value does not exist')
                Sleep(2500)
            Case 2
                If $Error = 1 Then _Splash('unable to open requested key')
                If $Error = 2 Then _Splash('unable to open requested main key ')
                If $Error = 3 Then _Splash('unable to remote connect to the registry')
                If $Error = -1 Then _Splash('unable to delete requested value')
                If $Error = -2 Then _Splash('unable to delete requested key/value')
                Sleep(2500)
        EndSwitch
    EndIf
    $i += 1
WEnd

Func _Splash($Text) ;Shows a small borderless splash message.
    SplashTextOn('', $Text, @DesktopWidth, 25, -1, 5, 33, '', 14)
EndFunc   ;==>_Splash
Use your example "EMAC" and populate the list with a few EMAC keys(e.g. EMAC, EMAC-1, EMAC Long) and try it out! Please let me know how this works for you! Edited by Varian

Share this post


Link to post
Share on other sites

try it out! Please let me know how this works for you!

Varian, it works perfectly. Thanks so much for taking the time to find the faults and code this. This is going to save a lot of time and effort clearing the the remote registry of machines. As it won't be run locally I'll try to remove the splash screen as the end user won't need to see this.

I'll be converting it to an .exe and use PsExec to run it remotely once it's copied over to the root of the machine.

Thanks again for your help.

vB


~ Make Hay while the Sun shines ~

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Varian, it works perfectly. Thanks so much for taking the time to find the faults and code this. This is going to save a lot of time and effort clearing the the remote registry of machines. As it won't be run locally I'll try to remove the splash screen as the end user won't need to see this.

I'll be converting it to an .exe and use PsExec to run it remotely once it's copied over to the root of the machine.

Thanks again for your help.

vB

No problem! You can comment out or remove all the "_Splash" commands and functions to remove them. Also, if you have remote registry enabled on the machines and you have Admin rights to the remote machines, you can add the computername before the HKLM to run the script from your workstation. If you have a list of the machines you can use a For...Next loop to scroll through them. Just change the line to something like this to test
$sKey = '\\RemoteComputerName\HKLM\SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-3'
Edited by Varian

Share this post


Link to post
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
Sign in to follow this  
Followers 0