Reputation: 1031
I am trying to figure out what version of my software is installed on user's computer. The versions are 5.0, 4.0, 3.0. Version 5.0 is the latest.
setlocal enabledelayexpansion
at the top.!variable!
to change the value of variable VERSION
in the IF loop. I am not able to make this work. Can you please help. Thank you so much.
Here is my script:
:: Find the version installed in the user's computer
:: valid versions are 5.0, 4.0, 3.0
setlocal enabledelayedexpansion
ECHO OFF
SET "error_code=0"
:: Latest Software version
SET VERSION=5.0
echo checking SOFTWARE Version: %VERSION%
:: build a path and check if it exists
SET "PATH=%PROGRAMFILES%\MYSOFT\%VERSION%"
call:CHECK_IF_VALID "%PATH%"
if %error_code% == 1 (
:: check v.4
SET VERSION=4.0
echo checking SOFTWARE Version: !VERSION!
SET "PATH=%PROGRAMFILES%\MYSOFT\%VERSION%"
call:CHECK_IF_VALID "%PATH%"
if %error_code% == 1 (
:: check v.3
SET VERSION=3.0
echo checking SOFTWARE Version: !VERSION!
SET "PATH=%PROGRAMFILES%\MYSOFT\%VERSION%"
call:CHECK_IF_VALID "%PATH%"
if %error_code% == 1 (
echo.&pause&goto:eof
)
)
)
:: Function to check if path exists
:CHECK_IF_VALID
if not exist %1 (
echo version not found...
set "error_code=1"
) else echo. Version found...
echo.
goto:eof
EXIT
Upvotes: 0
Views: 64
Reputation: 9545
Maybe something simpler :
Create a list
with all version, and loop
on all the version testing the path
.
@echo off
set "$version=3.0 4.0 5.0"
SET "$PATH=%PROGRAMFILES%\MYSOFT\"
::Checking if a version is installed
for %%a in (%$version%) do (
echo Checking Version : %%a
if exist "%$PATH%%%a" (
echo VERSION IS : %%a
goto:eof
)
)
echo No version Found !!
If various version can be installed on the same machine then we cath the last :
@echo off
set "$version=3.0 4.0 5.0"
SET "$PATH=%PROGRAMFILES%\MYSOFT\"
set "$LastVersion=none"
setlocal enabledelayedexpansion
::Checking if a version is installed
for %%a in (%$version%) do (
echo Checking Version : %%a
if exist "%$PATH%%%a" (
echo VERSION %%a is installed
set $LastVersion=%%a
)
)
Echo Last Version Installed : !$LastVersion!
Upvotes: 0
Reputation: 79983
path
refers to the path-sequence that windows searches to find an executable if it's not found in this directory. Not a good idea to change it. Not good at all.
::
is a broken label and labels are not allowed in (parenthesised sequences of commands) or code blocks
- use rem
instead.
You need to use !var!
within a code block
whenever you need to access the modified value of var
within that code block. %var%
accesses the original value of var
(when the code block was encountered)
Hence,
set "versionfound="
for %%v in (5.0 4.0 3.0) do if not defined versionfound (
if exist "%PROGRAMFILES%\MYSOFT\%%v" set "versionfound=%%v"
)
if defined versionfound (echo %versionfound% found) else (echo not found)
should detect the version. It substitutes the three possibilities into the string in turn and detects whether that version exists. If it does, versionfound
is set to the version that is found (having been initialised to nothing) and thereafter, the check is skipped because if defined
uses the current value in the environment (value set or value not set).
Upvotes: 1