如何阅读REG值到环境变量 [英] How to read a REG Value into an Environment Variable
问题描述
我想读一个REG值到一个BAT文件的环境变量。如,
REG HKLM \\系统\\ CurrentControlSet \\服务\\事件日志/ V的ImagePath
我可以得到REG命令的整个输出字符串转换成这样一个环境变量:
FOR / F有usebackq delims ==%%一中(`REG QUERYHKLM \\系统\\ CurrentControlSet \\服务\\事件日志/ V ImagePath`)DO设置的ImagePath =% %一个
然而,这是设置环境变量的整条生产线,即,
的ImagePath REG_EXPAND_SZ C:\\ WINDOWS \\ SYSTEM32 \\ Services.exe的
我想这个环境变量来只包含路径部分,即的。
C:\\ WINDOWS \\ SYSTEM32 \\ Services.exe的
感谢您的任何提示。
FOR / F令牌= 2 *%% A有('REG QUERYHKLM \\系统\\ CurrentControlSet \\服务\\事件日志/ v的ImagePath')DO设置的ImagePath = %%乙
默认的分隔符是空间和标签;没有必要去改变它。 TOKENS = 2 *存储在一个%%第二令牌,该行的%% B中的剩余部分。
关于你原来的code - 存在于您的情况使用有usebackq没有任何好处。此外,您的code不会preserve,如果它含有整行 =
,因为你使用的作为分隔符。
答案评论的
如果你能依靠的事实是,路径不包含任何空格,那么你可以简单地使用标记= 3,并保存%%一个。但我不会相信。相反,我会用
FOR / F令牌= 2 *%%的A('REG QUERYHKLM \\系统\\ CurrentControlSet \\服务\\事件日志/ V的ImagePath')DO(
为%% f由于(%% B)做(
设置的ImagePath = %%˚F
转到:突破
)
)
:打破
I am trying to read a REG Value into an Environment Variable in a BAT file. E.g.,
REG HKLM\SYSTEM\CurrentControlSet\Services\Eventlog /v ImagePath
I can get the entire output string of the REG command into an environment variable like this:
FOR /F "usebackq delims==" %%a in (`REG QUERY "HKLM\SYSTEM\CurrentControlSet\Services\Eventlog" /v ImagePath`) DO set ImagePath=%%a
However, this sets the environment variable to the entire line, i.e.,
ImagePath REG_EXPAND_SZ C:\Windows\system32\services.exe
I want the Environment Variable to consist only of the path portion, i.e.,
C:\Windows\system32\services.exe
Thanks for any tips.
for /f "tokens=2*" %%A in ('REG QUERY "HKLM\SYSTEM\CurrentControlSet\Services\Eventlog" /v ImagePath') DO set ImagePath=%%B
The default delimiter is space and tab; no need to change it. TOKENS=2* stores the 2nd token in %%A and the remainder of the line in %%B.
Concerning your original code - there is no benefit to using USEBACKQ in your situation. Also, your code would not preserve the entire line if it contained =
because you used that as the delimiter.
Answer to comment
If you can rely on the fact that the path does not contain any spaces, then you can simply use "TOKENS=3" and save %%A. But I wouldn't trust that. Instead, I would use
for /f "tokens=2*" %%A in ('REG QUERY "HKLM\SYSTEM\CurrentControlSet\Services\Eventlog" /v ImagePath') DO (
for %%F in (%%B) do (
set ImagePath=%%F
goto :break
)
)
:break
这篇关于如何阅读REG值到环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!