Reading a registry value to a batch variable, handling spaces in value

后端 未结 2 878
無奈伤痛
無奈伤痛 2021-02-05 15:51

I\'m struggling to read the value of a registry key into a variable. The registry value may, or may not, contain spaces. In this case I\'m trying to look up SDK paths.

I

相关标签:
2条回答
  • 2021-02-05 16:18

    Use Quotation marks

    SET "sdkpath=%%M"

    instead SET sdkpath=%%M

    This will avoid problems with spaces.

    0 讨论(0)
  • 2021-02-05 16:26

    Ah this is one of the annoying details about the for /f command. In order to read all the characters including the spaces with the * the previous token needs to be declared. tokens=2,* The functional reason is stated in the documentation.

    If the last character in the tokens= string is an asterisk (*), an additional variable is allocated and receives the remaining text on the line after the last token that is parsed.

    FOR Documentation

    FOR /F "usebackq tokens=2,* skip=2" %%L IN (
        `reg query "HKLM\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.1\WinSDKTools" /v InstallationFolder`
    ) DO SET sdkpath=%%M
    
    0 讨论(0)
提交回复
热议问题