REG DELETE a value which contains quotes in it, inside a batch file?

血红的双手。 提交于 2020-01-11 13:31:30

问题


I am trying to delete some registry keys in a batch file I made.

I found the following code on here and it works good until it hits the REG DELETE

for /F "tokens=1,*" %%a in ('REG QUERY "%KEY%" ^| findstr /I /C:"%VALUE%"') do (REG DELETE %KEY% /v %%a)

The value is located under [HK_CLASSES_ROOT\Installer\Assemblies\Global]
As you can probably see, most the values here have quotes in them for example:

ADODB,fileVersion="7.10.2346.0",version="7.0.3300.00",culture="neutral",publicKeyToken="B03F5F7F11D50A3A"

That's not exactly what I want to remove but it is pretty close.

So if I echo %%a it shows the value name as it is in the regedit with the quotes and everything. But as soon as it goes through REG DELETE (right now it asks for confirmation) the quotes are not there, so if I hit yes, it tells me it can't find the key.

Obviously it can't since it didn't parse the same thing it found initially. I've been trying to find a solution but I havn't found anything so far helping me in the right direction. I know I could probably do the same in vbs but this is a line long whereas in vbs it would be a lot longer to do the same job.

Any help is appreciated, if you need more info ask away, I just started messing around with batch file. I made this because I needed to automate the installation process of all the dependencies for my school project each time I work at school.(The computers are ghosted)

Thanks


回答1:


Note - Your "tokens=1,*" is not needed since you are only using the first token. But it is not causing a problem either

As you discovered and stated in your comment, the quotes must be escaped as \". You can use environment variable search and replace to programmatically escape the quotes. Since you must set and expand the variable within a code block, you must use delayed expansion. This is because normal expansion occurs when the line is parsed, and the entire block is parsed at once, so normal expansion will yield the value that existed before the loop was executed!. Delayed expansion occurs when the line is executed.

@echo off
setlocal enableDelayedExpansion

::some additional code to setup KEY and VALUE

for /F %%A in ('REG QUERY "%KEY%" ^| findstr /I /C:"%VALUE%"') do (
  set val=%%A
  REG DELETE "%KEY%" /v !val:"=\"!
)

I don't know if it is possible to have ! in the value, but if it is then you must toggle delayed expansion on and off within the loop. Otherwise expansion of %%A will be corrupted when it contains !.

@echo off
setlocal disableDelayedExpansion

::some additional code to setup KEY and VALUE

for /F %%A in ('REG QUERY "%KEY%" ^| findstr /I /C:"%VALUE%"') do (
  set val=%%A
  setlocal enableDelayedExpansion
  REG DELETE "%KEY%" /v !val:"=\"!
  endlocal
)


来源:https://stackoverflow.com/questions/13041700/reg-delete-a-value-which-contains-quotes-in-it-inside-a-batch-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!