【发布时间】:2014-02-11 19:34:57
【问题描述】:
我正在尝试将文本文件(它包含单行文本)的内容设置为变量。为此,我正在使用“set /p myvariable=
SetLocal EnableExtensions EnableDelayedExpansion
Set RegExist=No
Set RegUnins=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
Reg Query "%RegUnins%\{########-####-####-####-############}"
If "%errorlevel%"=="0" Set RegExist=Yes
If "%regexist%"=="Yes" (
Reg Query "%RegUnins%\{########-####-####-####-############}" /V "DisplayName" >"%temp%\appnam1.txt"
Find /I "This is the value for 'DisplayName'" "%temp%\appnam1.txt" >"%temp%\appnam2.txt"
More +2 "%temp%\appnam2.txt" >"%temp%\appnam3.txt"
Set /P _appnam=<"%temp%\appnam3.txt"
Set appnam=%_appnam:~24,53%
If "%appnam%"=="This is the value for 'DisplayName'" (
Echo - Current version installed: Performing reinstall...
Start "Reinstall" /B /High /Wait "\\server\installs\reinstall.exe"
) Else (
Echo - Previous version installed: Performing upgrade...
Start "Upgrade" /B /High /Wait "\\server\installs\upgrade.exe"
)
) Else (
Echo - Existing version not found: Performing new install...
Start "Install" /B /High /Wait "\\server\installs\new.install.exe"
)
但是,即使“%temp%\appnam3.txt”文件包含正确的文本,当我在代码中的这些点检查变量“%_appnam%”和“%appnam%”时,它们都是空白的。因此,如果机器确实有初始注册表项,它将始终执行升级过程而不是重新安装过程。有什么建议么?我不明白为什么“set / p”行不起作用。提前致谢。
【问题讨论】:
-
set /p工作正常。行不通的是它后面的行。您需要延迟扩展:Set appnam=!_appnam:~24,53!,因为您在(块内)。下一行也一样。 -
这里有一个很好的解释:stackoverflow.com/questions/1978088/…
标签: batch-file set