Brian
Brian

Reputation: 137

Batch file command to query numerical value of registry entry

I'm trying to get my install batch file to only run if the current version I'm installing is newer than the version installed on the local machine (if installed at all). I've been looking around and it seems like the common approach is to write to the registry, and then use that entry to key off for the future, by checking to see if it's there and the value.

I found this FOR statement here on this site that will check to see if a registry key equals a particular value, but I want the query to be able to determine if the registry value is less than a specified value. Is HKLM......\Installed_Reg_Key value (which is 4.5) less than 5.0. If I have to have two IF statements, one to check to see if the key is there (if it's not currently installed) and another to check to see if it's less than what I'm pushing, that's fine. And if I have to write a particular type of reg key, I could care less.

Thanks, Brian

Upvotes: 1

Views: 2955

Answers (1)

RobW
RobW

Reputation: 444

See code below -

You just need a single FOR statement to test - as it will populate an environment variable. If the variable does not exist, then the program is not installed, and you :CALL an appropriate action.

The code below:

  1. Queries the registry for the value in HKLM\software\Batchver\batchver
  2. Assigns it to an environment variable.
  3. Tests if assignment was successful. If assignment fails (ie no data in registry entry), call the installation routine.
  4. Three more statements test the value of the environment variable, and take action each time.
  5. All subroutines contain a routine to update the registry to the current version number.

@echo off SETLOCAL ENABLEEXTENSIONS SETLOCAL ENABLEDELAYEDEXPANSION set batchver=

for /f "tokens=3 skip=3" %%i in ('reg query HKLM\Software\batchver /v batchver') do @set batchver=%%i


if .batchver==. call :new_install

:: if already installed do nothing.

if batchver==5.0 @echo Up to date!  && goto :exit

if batchver leq 4.5 call :reinstall

if batchver gte 4.6 call :patch



goto :exit


:new_install

  <new installation routines here.>
  ::update registry with new version
  reg add HKLM\software\batchver /v batchver /d 5.0 /f


goto :eof

:reinstall
  <reinstall routine here>
::update registry with new version
  reg add HKLM\software\batchver /v batchver /d 5.0 /f
goto :eof

:patch
  <patch routines here>
::update registry with new version
  reg add HKLM\software\batchver /v batchver /d 5.0 /f
goto :eof

:exit

Upvotes: 1

Related Questions