Prevent an old setup to overwrite a new version

Post specialized setup scripts you have created here.
zchris
Posts: 92
Joined: Wed Feb 23, 2005 3:16 am
Location: Austria

Prevent an old setup to overwrite a new version

Postby zchris » Tue Sep 06, 2005 7:44 am

InstallAware can create setups that automatically uninstall the previous version (take a look at the NEEDSUPGRADE variable in the samples).

Unfortunately it does not detect if the installed version is actually newer than the version you are trying to install.

This can be fixed easily. However it still took me half an hour to implement so I thought I'd share it with you.

Add this to the beginning of your script:

Code: Select all

Set Variable VERTODAY to 2005/09/06 10:44

Insert this into your NEEDSUPGRADE branch:

Code: Select all

if Variable NEEDSUPGRADE Equals TRUE
  // check installed version
  Read Registry Key HKLM\\SOFTWARE\\Company\\Setups\\$TITLE$ into OLDTODAY
  if Varialbe OLDTODAY Greater Than $VERTODAY$
    MessageBox: Newer Version Detected, A newer version of $TITLE$ is already installed.$NEWLINE$$NEWLINE$Setup will exit.
    Terminate Installation
  end
  // do uninstall
  // ...
end

Insert before Apply Install:

Code: Select all

// save setup version
Write Registry Key HKLM\\SOFTWARE\\Company\\Setups\\$TITLE$, $VERTODAY$


You only need to set VERTODAY to the current date. If you are building from the commandline you can update it automatically with this C# app:

Code: Select all

using System;
using System.IO;
using System.Text;

namespace UpdateVerToday 
{

  class Class1
  {

    [STAThread]
    static void Main(string[] args)
    {
      if (args.Length<1)
      {
        Console.WriteLine("Specify a mia file.");
        return;
      }

      string name=args[0];
      string text;

      Console.WriteLine("Processing file {0}", name);

      using (StreamReader input=new StreamReader(name, Encoding.ASCII))
        text=input.ReadToEnd();

      const int nameLen=8, verLen=16;

      int idx=text.IndexOf("VERTODAY");
      if (idx<0) throw new Exception("Invalid file (no VERTODAY).");
      if (text[idx-1]!='\\n' || text[idx+nameLen]!='\\r')
        throw new Exception("Invalid file (invalid VERTODAY).");

      idx+=nameLen+2; // skip name, lf
      string oldVer=text.Substring(idx, verLen), newVer=DateTime.Now.ToString("yyyy/MM/dd HH:mm");

      Console.WriteLine("Old Version: {0}", oldVer);
      Console.WriteLine("New Version: {0}", newVer);

      string text2=text.Substring(0, idx);
      text2+=newVer;
      text2+=text.Substring(idx+verLen);

      File.Copy(name, name+".verbak", true);

      using (StreamWriter output=new StreamWriter(name, false, Encoding.ASCII))
        output.Write(text2);

      Console.WriteLine("done.");
    }
  }
}

Return to “Sample Scripts”

Who is online

Users browsing this forum: No registered users and 22 guests