File comparision: Rip apart getFileModDate, or is there an easier way?


I’d like to be able to check the dates between two files to see if one is newer than the other. Other than ripping apart the getFileModDate into seconds and seeing which total is higher (which is what i’m currently doing), is there an easier way? I’d hoped some fancy dotNet thing would have helped me, but so far the only file comparision examples i’ve seen are comparing the contents of the files, not the date and time.


That’s a really intersting idea…I hadn’t thought of trying that until you mentioned, thanks…

Now, I haven’t tried it, but .Net has a class “System.IO.File” with a method “getCreationTime()” which returns a “DateTime” struct, which you can get a “tick” property from which “represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, Janurary 1, 0001”…

I’d imagin you could use the dotnetclass method to instance of the class

myFile = dotnetclass "System.IO.File" "C:\..."

I’ve not tried this at all in any fashion, but it should get you started…



Yes the dotNet methods work great.

use dotNetClass to create an instance of it.


Thanks guys, I’ll check it out.


I think “System.IO.FileInfo” would be better : see CreationTime, LastAccessTime, LastWriteTime properties for example.


I always wondered whether we could use something like CRC for comparing files, but haven’t found any tool to do so…

Any ideas?
– MartinB


So, for example, would I call the fucntion like this?

 theFile = maxFilePath + maxFileName
 myFile = dotnetclass "System.IO.FileInfo" theFile
 value = myFile.LastWriteTime

Or would I need to declare the value variable as a DateTime?

As it stands i’m getting some errors even at the open level.

Sorry for all the questions… I’m still trying to get my head around how dotNet declaration is handled in MXS! I hope someone else is getting something out of this discussion! :slight_smile:


No, if the value is returned from a method you don’t need to create it.

You can get the LastWriteTime information like this:

FileName = maxFilePath + maxFileName
FileInfo = dotNetObject "System.IO.FileInfo" FileName
Hour = (FileInfo.LastWriteTime.Hour as string) + ":" + (FileInfo.LastWriteTime.Minute as string) + ":" + (FileInfo.LastWriteTime.Second as string)
Date = FileInfo.LastWriteTime.DayOfWeek.ToString() + " " + (FileInfo.LastWriteTime.Month as string) + "\\" + (FileInfo.LastWriteTime.Day as string) + "\\" + (FileInfo.LastWriteTime.Year as string)
Print "Last Write Time on " + Date + " at " + Hour


Way cool, I was trying to work out how to pass parameters to constructors! Cheers!


Brilliant, thanks Yannick! Out of interest, I couldn’t find the hour/minute/second part in the MSDN reference in relation to lastWriteTime. Where do these properties come from? Is it just something inherent from the DateTime object, or is it something from the FileInfo properties?


There apart of the DateTime class, check out for more details


Ah, yes just found it, silly me! :smiley:

So like you were saying earlier, all I would have to do is compare fileA’s and fileB’s
FileInfo.LastWriteTime.Ticks to get what i’m after?


That’s how I’d see it. Seen as ticks is a long value, you should be able to compare the value to determine which file is “younger”

if file1.GetLastWriteTime().ticks < file2.GetLastWriteTime().ticks then
    --File 1 is younger

ps - Haven’t tested this…



Brilliant, that just saved me about 10 lines of code!
Really, really appreciate it guys. This is why I love this forum.:smiley:

One day i’ll finally get my head around the dotNet interfacing. This has been a fantastic learning experience so far.

ps - Haven’t tested this…

Don’t worry, it seems to work fine. :slight_smile:


I gotta tell ya, I’m not a big microsoft fan, but the dotnet api kicks the old activex api’s butt. The whole API tends to revolve around a common design paradigm, which can make it eaiser to learn over time - the more you play with it, the eaiser to gets.


There’s also the Compare() method. It’s static so you don’t need to call it over an object. Just get DateTime class using dotNetClass:

DateTime = dotNetClass "System.DateTime"
date1 = fileInfo1.GetLastWriteTime()
date2 = fileInfo2.GetLastWriteTime()
compare = DateTime.Compare date1 date2
if compare < 0 then
	Print "date1 is less than date2"
else if compare == 0 then
	Print "date1 equals date2"
else -- compare > 0
	Print "date1 is greater than date2"



I just had a thought…maybe we should add some of this to the dotnet sticky thread!


This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.