将注册表值读取到批处理变量中,处理值中的空格 [英] Reading a registry value to a batch variable, handling spaces in value

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

问题描述

我正在努力将注册表项的值读入变量.注册表值可以包含空格,也可以不包含空格.在这种情况下,我正在尝试查找SDK路径.

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.

使用reg query可以很容易地获得值,但是它以非常无用的格式返回:

It's easy to get the value with reg query, but it's returned in a remarkably unhelpful format:

C:\Users\Administrator\Desktop>reg query "HKLM\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.1\WinSDKTools" /v InstallationFolder

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.1\WinSDKTools
    InstallationFolder    REG_SZ    C:\Program Files\Microsoft SDKs\Windows\v7.1\bin\

键名,键类型和键值由一系列空格而不是制表符分隔.

The key name, key type, and key value are separated by a series of spaces, not tabs.

您认为您可以使用类似的东西:

You'd think you could use something like:

FOR /F "usebackq tokens=3* skip=2" %%L IN (
    `reg query "HKLM\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.1\WinSDKTools" /v InstallationFolder`
) DO SET sdkpath=%%L

...但是如果键包含空格(在这种情况下),则在命令行上发出的结果是:

... but if the key contains spaces, as in this case, the result emitted on the command line is:

C:\Users\Administrator\Desktop>SET sdkpath=C:\Program

这没有帮助.似乎没有通配符变量表示所有匹配项".而且你不能只写:

Which isn't helpful. There doesn't seem to be a wildcard variable to say "All matches". And you can't just write:

DO SET sdkpath=%%L %%M

...,因为如果路径中有 not 个空格,则将产生文字%M(并且还将产生尾随空格).

... because if there are not spaces in the path, that will produce a literal %M (and would also produce a trailing space).

所以.有没有办法在批处理文件中执行此简单操作?

So. Is there any way to do this simple thing in a batch file?

我已经在Powershell(和一些Perl)中编写了大部分工具,但是我需要使用批处理文件来执行简单"任务,即call编写visual studio/windows sdk环境脚本,然后调用设置好环境后,即可使用健全的语言编写代码.

I've written most of my tooling in Powershell (and some Perl), but I need to use a batch file for the "simple" task of calling the visual studio / windows sdk environment script and then invoking the code in sane languages once the environment is set up.

您会认为在cmd.exe存在十多年之后,并且在command.com之前,这很容易.帮助吗?

You'd think that after 10+ years of cmd.exe's existence, and command.com before it, this would be easy. Help?

(我可以使用Perl和Win32::包来查询注册表,但这并不能帮助我将其放入批处理变量中...)

(I can use Perl and the Win32:: packages to query the registry, but it doesn't help me get it into a batch var...)

使用Win2k12.

我读过:

  • Read registry value that contains spaces using batch file
  • Shell script reading windows registry

和许多其他方法,但没有一种方法能处理一般情况,该方法通常是在不知道键是否包含空格/多少的情况下从键中正确读取值的.

and many others, but none handle the general case of correctly reading a value from a key without knowing whether or not it contains spaces / how many.

推荐答案

啊,这是关于for /f命令的烦人的细节之一.为了读取所有字符,包括带有*的空格,需要声明上一个标记. tokens=2,*功能原因已在文档中说明.

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.

如果tokens =字符串中的最后一个字符是星号(*),则会分配一个附加变量,并在解析的最后一个令牌之后的行中接收其余文本.

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文档

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

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

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