0

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.

  • I would like to check if the user has any of these versions installed in this order.
  • I have wrote a script as follows: I also added setlocal enabledelayexpansion at the top.
  • I have learnt here (on stackoverlow) that I need to use !variable! to change the value of variable VERSION in the IF loop.
  • I tried it myself, but most of the examples were for echo'ing.
  • Here I am changing the value of the version.
  • Then update the path directory.

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    
6
  • You should not change the general path variable - choose a different name for your app. If you want to change the path variable persistently edit the registry or use setx and leave the present entries in the path unless you now what you are doing. Commented Jun 23, 2017 at 17:16
  • Are \MYSOFT\5.0, \MYSOFT\4.0 and MYSOFT\3.0 supposed to be file names or directory names? Commented Jun 23, 2017 at 17:52
  • @LotPings I apologize I should have used a better variable name. (I do have a better name). I just was trying to strip off all additional things to make it available for Stackoverflow question. Thank you. Commented Jun 23, 2017 at 19:06
  • @Cricrazy, could you answer my question please! Commented Jun 25, 2017 at 11:27
  • @Compo they \mysoft\5.0, mysoft\4.0 etc are the folder names. Commented Jun 29, 2017 at 13:38

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

Comments

0

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!

2 Comments

What is the purpose of include the not-alpha $ char at beginning of variable names? I can see several disadvantages on doing that and not a single advantage...
This is only a personal way of defining variables that make the code more readable. I don't see any disadvantages to that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.