阅读用空格分隔的单词字符串值还包含批处理脚本中的空格 [英] Read words separated by space & string value also contains space in a batch script
问题描述
我需要从批处理脚本中读取注册表的默认值.某些商品的名称包含一些空格.我也想在批处理文件中执行两次for循环.
I need to read default value of registry from a batch script. The name of certain item's contains some spaces. Also I want to execute for loop in batch file for one two times.
rem @echo OFF
setlocal ENABLEEXTENSIONS
set KEY_NAME="HKEY_CURRENT_USER\Software\abc\xyz pqr"
FOR /F "tokens=1-3 delims=<TAB>" %%A IN ('REG QUERY %KEY_NAME% 2^>nul') DO (
set ValueName=%%A
set ValueType=%%B
set ValueValue=%%C
)
if defined ValueName (
@echo Value Name = %ValueName%
@echo Value Type = %ValueType%
@echo Value Value = %ValueValue%
) else (
@echo %KEY_NAME%\%VALUE_NAME% not found.
)
pause
它提供以下输出
rem @echo OFF
setlocal ENABLEEXTENSIONS
set KEY_NAME="HKEY_CURRENT_USER\Software\abc\xyz pqr"
FOR /F "tokens=1-3 delims=<TAB>" %A IN ('REG QUERY "HKEY_CURRENT_USER\Software\abc\xyz pqr" 2>nul') DO (
set ValueName=%A
set ValueType=%B
set ValueValue=%C
)
(
set ValueName=HKEY_CURREN
set ValueType=_USER\Software\abc\xyz pq
set ValueValue=
)
(
set ValueName= (Default) REG_SZ C:\Program Files (x86)\abc\
set ValueType=
set ValueValue=
)
(
set ValueName=
set ValueType=uthor REG_SZ gj
set ValueValue=
)
(
set ValueName= Version REG_SZ 1.4.0.0
set ValueType=
set ValueValue=
)
if defined ValueName (
) else ()
Value Name = Version REG_SZ 1.4.0.0
Value Type =
Value Value =
pause
Press any key to continue . . .
我想获得以下输出,并且我也希望一旦获得(Default)的值,此for循环就应该停止
I would like to get following output and also want that this for loop should stop once I get value of (Default)
(
set ValueName=(Default)
set ValueType=REG_SZ
set ValueValue=C:\Program Files (x86)\abc\
)
提前感谢您的帮助!
推荐答案
在Windows XP和更高版本的Windows上,此批处理批处理代码应适用于此任务.
This commented batch code should work for this task on Windows XP and later Windows versions.
@echo off
rem On Windows Vista and later REG.EXE outputs without version info:
rem HKEY_CURRENT_USER\Software\abc\xyz pqr
rem (Default) REG_SZ C:\Program Files (x86)\abc\
rem There are only spaces used to separate value name, value type and value string.
rem But REG.EXE version 3.0 outputs on Windows XP with version info:
rem ! REG.EXE VERSION 3.0
rem
rem HKEY_CURRENT_USER\Software\abc\xyz pqr
rem <NO NAME> REG_SZ C:\Program Files (x86)\abc\
rem NOTE: There are 4 indent spaces and 2 separating tabs in REG 3.0 output line.
rem So either token 2 or token 3 contains value type REG_SZ
rem used to identify the line with the wanted information.
set "TypeToken=2"
:GetPathFromRegistry
for /F "skip=1 tokens=%TypeToken%*" %%A in ('%SystemRoot%\System32\reg.exe QUERY "HKCU\Software\abc\xyz pqr" /ve 2^>nul') do (
if "%%A" == "REG_SZ" (
set "AppPath=%%~B"
goto HaveAppPath
) else if "%%A" == "NAME>" (
set "TypeToken=3"
goto GetPathFromRegistry
)
)
echo Failed to read application path from registry.
pause
goto :EOF
:HaveAppPath
echo Application path is: %AppPath%
pause
在Windows VISTA和更高版本的Windows上, FOR 循环仅处理 REG 输出的一行.
On Windows VISTA and later Windows versions the FOR loop processes only 1 line from output of REG.
在Windows XP和Windows Server 2003上, FOR 循环使用tokens=X*
的不同值运行两次,并在获取感兴趣的字符串值之前处理更多的行,这是因为reg.exe
3.0版.
On Windows XP and Windows Server 2003, the FOR loop is run twice with different values for tokens=X*
and processes more lines before getting the string value of interest because of the version information output by reg.exe
version 3.0.
要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
-
echo /?
-
for /?
-
goto /?
-
if /?
-
pause /?
-
reg /?
-
reg query /?
-
set /?
echo /?
for /?
goto /?
if /?
pause /?
reg /?
reg query /?
set /?
这篇关于阅读用空格分隔的单词字符串值还包含批处理脚本中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!