使用“set / p myvariable =

时间:2014-02-11 19:34:58

标签: batch-file set

我正在尝试将文本文件的内容(它包含一行文本)设置为变量。为此,我正在使用“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”行不起作用。提前谢谢。

1 个答案:

答案 0 :(得分:2)

使用延迟扩展。 我尝试在下面重现你的情况。似乎变量设置为延迟,如果您尝试多次运行脚本,则将设置值。

@echo off
setlocal enableDelayedExpansion
set regexist=Yes

If "%regexist%"=="Yes" (
    Set /P _appnam=<"C:\result.csv"
    echo !_appnam!
    Set appnam=!_appnam:~24,53!
    echo !appnam!
)
相关问题