PDA

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


erilaz
10-28-2007, 11:57 PM
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.

RustyKnight
10-29-2007, 12:36 AM
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 classmyFile = dotnetclass "System.IO.File"
myFile.open "C:\..."
myFile.getCreationDate()
I've not tried this at all in any fashion, but it should get you started...

Shane

PEN
10-29-2007, 11:14 AM
Yes the dotNet methods work great.

use dotNetClass to create an instance of it.

erilaz
10-29-2007, 11:46 AM
Thanks guys, I'll check it out.

ypuech
10-29-2007, 03:10 PM
I think "System.IO.FileInfo" would be better : see CreationTime, LastAccessTime, LastWriteTime properties for example.

martinB
10-29-2007, 06:25 PM
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

erilaz
10-29-2007, 11:22 PM
So, for example, would I call the fucntion like this?

theFile = maxFilePath + maxFileName
myFile = dotnetclass "System.IO.FileInfo"
myFile.open 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! :)

ypuech
10-29-2007, 11:47 PM
Or would I need to declare the value variable as a DateTime?
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

RustyKnight
10-29-2007, 11:52 PM
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 " + HourWay cool, I was trying to work out how to pass parameters to constructors! Cheers!

erilaz
10-30-2007, 12:29 AM
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?

RustyKnight
10-30-2007, 12:32 AM
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 http://msdn2.microsoft.com/en-us/library/system.datetime.aspx for more details

erilaz
10-30-2007, 12:35 AM
There apart of the DateTime class, check out http://msdn2.microsoft.com/en-us/library/system.datetime.aspx for more details

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

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?

RustyKnight
10-30-2007, 12:39 AM
Ah, yes just found it, silly me! :D

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

Shane

erilaz
10-30-2007, 12:44 AM
Brilliant, that just saved me about 10 lines of code!
Really, really appreciate it guys. This is why I love this forum.:D

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

RustyKnight
10-30-2007, 12:48 AM
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.

ypuech
10-30-2007, 09:02 AM
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"

RustyKnight
10-30-2007, 10:51 AM
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"Nice...

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

CGTalk Moderation
10-30-2007, 10:51 AM
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.