Search code examples
batch-fileregistry

Batch file reg delete : how to delete registry key with space in name and backslash ending


I do a search :

set keypath=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders
reg query "!keypath!" /s /f "%mysearch%"

and i want to delete all the result that match with mysearch

But i have a problem with some results (!myresult!) like this :

"c:\Program Files\mysearch\"
"c:\Program Data\mysearch\"

when i do :

reg delete "!keypath!" /v "!myresult!" /f

it's not working (i think because !myresult! ending with \ and "), asking me confirmation before deleting and don't work because the system no find key

when i do :

set myresult=!myresult:\=\\!
reg delete "!keypath!" /v "!myresult!" /f

it's not working, not asking me confirmation but the system no find key too

when i do :

reg delete "!keypath!" /v !myresult! /f

it's not working i have a syntax error (because of space in !myresult!), but with no space in !myresult! it works

If somebody have an idea.

Thanks.


Solution

  • Your ending backslash is escaping the double quote (more here).

    You can check if the value ends in a slash and double it.

    if "!myresult:~-1!"=="\" set "myresult=!myresult!\"
    reg delete "!keypath!" /v "!myresult!" /f
    

    But this will fail when you have a double backslash ended value, so probably the better option could be to directly use

    reg delete "!keypath!" /f /v "!myresult!
    

    The /f has been moved AND the closing quote has been removed. To use this, ensure there is not any space at the end of the line. As the quote has not been closed, any space will be included inside the "quoted" value.