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

前端 未结 1 637
礼貌的吻别
礼貌的吻别 2021-01-28 06:58

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         


        
相关标签:
1条回答
  • 2021-01-28 07:33

    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
    )
    
    0 讨论(0)
提交回复
热议问题