包含空格使用批处理文件中读取注册表值 [英] Read registry value that contains spaces using batch file

查看:722
本文介绍了包含空格使用批处理文件中读取注册表值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理文件,读取注册表值。

I have a batch file that reads the registry value.

不过,我读条目包含空格,我只好像我的设置变量时的第一个空格字符之前捕捉到一切。

However the entry that I am reading contains spaces and I only seem to capture everything before the first space character when setting my variable.

set KEY_NAME="HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\My Entry"
set VALUE_NAME=Home

FOR /F "usebackq skip=2 tokens=1-3" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
    set ValueName=%%A
    set ValueType=%%B
    set Home=%%C
)

if defined ValueName (
    @echo Home = %Home%
) else (
    @echo %KEY_NAME%\%VALUE_NAME% not found.
)

家注册表项实际上包含此字符串:C:\\ Program Files文件(x86)的\\方向1 \\方向2和批处理文件只抓住了这个:C:\\ PROGRAM

The home registry entry actually contains this string: "C:\Program Files (x86)\Dir1\Dir2" and the batch file only captures this: C:\Program

没有任何人对如何解决这一问题的想法?

Does anybody have an idea of how to fix this?

感谢

推荐答案

此问题是因为该字符串包含空格和字符串(可能更多的部分,如果有多个空格)被视为另一种令牌的第二部分(4 5,等)。为了解决这个问题,通过该行的其余部分%%下与这样一个星号:

This problem happens because the string contains spaces and the second part of the string (possibly more parts if there are more spaces) are treated as another token (in 4, 5, etc.) To fix this, pass the rest of the line to %%C with an asterisk like this:

FOR /F "usebackq skip=2 tokens=1,2*" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
    set ValueName=%%A
    set ValueType=%%B
    set Home=%%C
)

星号( * )指该行的其余部分通入下一个变量(与所有的空格)。

The asterisk (*) means to pass the rest of the line into the next variable (with all the spaces).

这篇关于包含空格使用批处理文件中读取注册表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆