PDA

View Full Version : Compare File Versions - String to Number Problems


Dnixon
01-10-2008, 05:50 PM
I am trying to compare which file version of my software is installed to what the latest version out is so I can let True Update send the appropriate incremental patch to the user. It seems so simple, but for some reason my code does not return the expected values.

I would expect that the code below would trim the "1." from the left of the g_InstalledVersion string and the ".0.0" from the right of that string leaving me with a string containing the value "10". I do the same with my 1.11.0.0 string and then convert both of those strings to numbers to perform my arithmetic. I am expecting to have a final result of 1 stored in my versions_difference variable because 11 - 10 = 1, but it doesn't happen. It's failing somewhere with the String.TrimRight and String.TrimLeft function and I have no clue why. I'm sure this is really simple, but I just can't figure out what is going wrong. Does anyone have any ideas? Attached is my code with some debugging dialog messages I've commented out.

g_InstalledVersion = "1.10.0.0";
--Dialog.Message("g_InstalledVersion", g_InstalledVersion);

g_TargetVersion = "1.11.0.0";
--Dialog.Message("g_TargetVersion", g_TargetVersion);


trim_InstalledVersion = String.TrimRight(g_InstalledVersion, ".0.0");
--Dialog.Message("trim_InstalledVersion", trim_InstalledVersion);

trim_InstalledVersion2 = String.TrimLeft(trim_InstalledVersion, "1.");
--Dialog.Message("trim_InstalledVersion2", trim_InstalledVersion2);

trim_TargetVersion = String.TrimRight(g_TargetVersion, ".0.0");
--Dialog.Message("trim_TargetVersion", trim_TargetVersion);

trim_TargetVersion2 = String.TrimLeft(trim_TargetVersion, "1.");
--Dialog.Message("trim_TargetVersion2", trim_TargetVersion2);

short_InstalledVersion = String.ToNumber(trim_InstalledVersion2);
--Dialog.Message("short_InstalledVersion", short_InstalledVersion);

short_TargetVersion = String.ToNumber(trim_TargetVersion2);
--Dialog.Message("short_TargetVersion", short_TargetVersion);

versions_difference = (short_TargetVersion - short_InstalledVersion);

Dialog.Message("versions_difference", versions_difference);


Thanks for any help anyone can offer! :)

Dnixon
01-11-2008, 08:58 AM
I figured it out :D

I found out about the bug with the string.trimRight / string.trimLeft function and rewrote the code as follows. Works great for determining which incremental patch to deliver to the user.

g_installedVersion could be a registry key value that is set when you install the software. True Update can request that key and set it to the g_installedVersion variable at runtime. g_targetVersion is coded in the server script in your True Update code.


g_installedVersion = "1.1.0.0";

t_installedVersion = String.Replace(g_installedVersion, ".0.0", "", false);
t1_installedVersion = String.Replace(t_installedVersion, "1.", "", false);


g_targetVersion = "1.4.0.0";
t_targetVersion = String.Replace(g_targetVersion, ".0.0", "", false);
t1_targetVersion = String.Replace(t_targetVersion, "1.", "", false);

i_version = (t1_targetVersion - t1_installedVersion);

if i_version == 1 then

-- Where do you want to download the installer/patch file from and to?
g_SourceURL = "www.whatever.com/incrementalPatch1.exe";
g_PatchFileDest = SessionVar.Expand("%SourceFolder%\\Patches\\incrementalPatch1.exe");

else if specific_version == 2 then

-- Where do you want to download the installer/patch file from and to?
g_SourceURL = "www.whatever.com/incrementalPatch2.exe";
g_PatchFileDest = SessionVar.Expand("%SourceFolder%\\Patches\\incrementalPatch2.exe");

else

-- Where do you want to download the installer/patch file from and to?
g_SourceURL = "www.whatever.com/incrementalPatch3.exe";
g_PatchFileDest = SessionVar.Expand("%SourceFolder%\\Patches\\incrementalPatch3.exe");

end
end

Adam
01-14-2008, 12:53 PM
I am not aware of any current bugs with the String functions. Would you be able to post your findings?

Thanks for your time

Adam Kapilik

Dnixon
02-04-2008, 05:55 PM
If I remember correctly, it just doesn't work. I found some other threads where people said there was a bug with the function. Here's the string I had to begin with:

"1.1.0.0"

I wanted to remove the ".0.0" from the right and the "1." from the left so I would just have a number: "1". Well it didn't actually trim the characters when the code was executed.

Mark
02-05-2008, 12:23 PM
Hi Dnixon,

Remember that the second parameter passed to String.TrimLeft() and String.TrimRight() are the characters to trim not the string to trim.

So in this example:

trimmed = String.TrimRight("1.10.0.0", "0.");

The result would be:

1.1

This is because we are trimming the characters '.' and '0' from "1.10.0.0". We are not trimming the string ".0".

So in your example when you trim ".0.0" from the right, and "1." from the left, you are actually trimming all of the characters from the string.

I hope this helps.