Page 1 of 1
Delect Registry Key
Posted: 21 Feb 2023 16:43
by Docfxit
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 (169.72 KiB) Viewed 5871 times
When I run it this is what I get:
- InvalidSyntax.jpg (70.15 KiB) Viewed 5871 times
What can I change so i don't get Invalid Syntax?
Re: Delect Registry Key
Posted: 21 Feb 2023 17:12
by penpen
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
Re: Delect Registry Key
Posted: 21 Feb 2023 17:39
by Docfxit
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.
Re: Delect Registry Key
Posted: 21 Feb 2023 18:35
by Docfxit
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.
Re: Delect Registry Key
Posted: 22 Feb 2023 10:56
by Compo
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.
Re: Delect Registry Key
Posted: 22 Feb 2023 11:17
by Docfxit
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.