1

I'm trying to set the contents of a text file (it contains a single line of text) to a variable. To do this, I'm utilizing "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"
)

However, even though the "%temp%\appnam3.txt" file contains the correct text, when I check the variables "%_appnam%" and "%appnam%" at those points in the code, they are blank. Therefore, if the machine does have the initial registry entry, it will always perform the upgrade process instead of the reinstall process. Any suggestions? I don't understand why the "set /p" line isn't working. Thanks in advance.

user3208239
  • 691
  • 1
  • 7
  • 15
  • 2
    the `set /p` IS working. What doesn't work is the line after it. You need delayed expansion there: `Set appnam=!_appnam:~24,53!` because you are inside a `(` block `)` . Same thing in the next line. – Stephan Feb 11 '14 at 19:53
  • Here is a quite good explanation: http://stackoverflow.com/questions/1978088/batch-file-fails-to-set-variable-in-if-clause?rq=1 – Stephan Feb 11 '14 at 19:59

1 Answers1

3

PLease use delayed expansion. I tried to reproduce your case below. It seems the variable are set delayed, If you'll try to run you script several times, the values will be set.

@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!
)
mihai_mandis
  • 1,578
  • 1
  • 10
  • 13