Delect Registry Key

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Docfxit
Posts: 132
Joined: 12 Nov 2015 12:42

Delect Registry Key

#1 Post by Docfxit » 21 Feb 2023 16:43

I'd like to delete a registry key. This is what I'm using:

Code: Select all

REG Delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders" /v c:\Program Files\Common Files\Corel\ 
This is the registry key:
notFound.jpg
notFound.jpg (169.72 KiB) Viewed 5908 times
When I run it this is what I get:
InvalidSyntax.jpg
InvalidSyntax.jpg (70.15 KiB) Viewed 5908 times
What can I change so i don't get Invalid Syntax?

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Delect Registry Key

#2 Post by penpen » 21 Feb 2023 17:12

I actually can't test it, but it might be the space characters, that break the value early, so you might encapsulate the value "c:\Program Files\Common Files\Corel\" in doublequotes.

penpen

Docfxit
Posts: 132
Joined: 12 Nov 2015 12:42

Re: Delect Registry Key

#3 Post by Docfxit » 21 Feb 2023 17:39

Thank you for the reply...

I had it in quotes originally. I tried it again and came up with different results.

Code: Select all

C:\WINDOWS\system32>Echo Y   | REG Delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders" /v "c:\Program Files\Common Files\Corel\"
Delete the registry value c:\Program Files\Common Files\Corel" (Yes/No)? ERROR: The system was unable to find the specified registry key or value.
I do run it as administrator
My user does have administrator rights.

Docfxit
Posts: 132
Joined: 12 Nov 2015 12:42

Re: Delect Registry Key

#4 Post by Docfxit » 21 Feb 2023 18:35

I tried this:

Code: Select all

C:\WINDOWS\system32>Echo Y  > REG Delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders" /v "c:\Program Files\Common Files\Corel\"
After the Echo Y
I changed the | to >
With this I don't get an error but it doesn't delete the registry entry.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Delect Registry Key

#5 Post by Compo » 22 Feb 2023 10:56

The first observation I made, is that you clearly have not read the help and usage information for the command utility you are using. If you open a Command Prompt window, type reg delete /? and press the ENTER key, you'll see that there is an option which Forces the deletion without prompt., i.e. /f. So you most certainly do not need to pipe or redirect confirmation to the command.

The main issue with your code is with the trailing backward slash in your value name. In order to counteract that, the simplest way is to escape it with another backward slash.

Code: Select all

@%SystemRoot%\System32\reg.exe Delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders" /V "%CommonProgramFiles%\Corel\\" /F >NUL 2>&1
Personally, I prefer to validate that an entry exists before trying to perform an action, like deletion, on it:

Code: Select all

@Echo Off

Set "Key=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders"
Set "Val=%CommonProgramFiles%\Corel\\"

Set "Reg=%SystemRoot%\System32\reg.exe"
(%Reg% Query "%Key%" /V "%Val%" && %Reg% Delete "%Key%" /V "%Val%" /F) >NUL 2>&1
Please remember that because this is a protected key, the batch file, or command would need to be invoked elevated.

Docfxit
Posts: 132
Joined: 12 Nov 2015 12:42

Re: Delect Registry Key

#6 Post by Docfxit » 22 Feb 2023 11:17

That worked super. Very well done.

I did read the help. I did try the /f with and without the Echo Y. I tried many combinations. Nothing worked.
the option I didn't try was the escaped slashes \\.
The way you put it together with the Query to see if it exists before trying to perform an action is really great.
Thank you very much for the great solution.

Post Reply