Detect file has changed

I’m drawing a blank here… I need something simple to detect if a file has changed between the time an app loses focus and regains it

Example… my app gets a Folderitem to a file and displays that file… the user then opens another app and uses that app to change and save the file… when my app gets focus back I need someway to check if something changed so I can ask the user if they wish to reload the data

Doesn’t need to be a realtime event… just something in GotFocus perhaps

hmmm. just had an idea… .but its really late

function didFileChange(oldPath as folderitem) as boolean
dim f as folderitem=getfolderitem(oldpath.nativepath)
return oldPath.modificationdate<>f.modificationdate
end function

I am way to tired to test this… but it just popped into my head

comments?

g’nite ZZZZzzzz :slight_smile:

FSEvents in macOS: https://en.wikipedia.org/wiki/FSEvents

Here’s the Apple documentation.

https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/FSEvents_ProgGuide/Introduction/Introduction.html?language=objc#//apple_ref/doc/uid/TP40005289

As Beatrix and Sam suggested, FSEvents is the most powerful solution. It can even keep track of modifications when your app is down. However, it is a bit complex to use.

If you want something easier to implement, you might want to compute the MD5 hash of the file when your app becomes frontmost and compare it to its previous hash. In a simple synchronous Shell, use:

md5 -q /path/to/the/file

It will just print out the MD5 hash. Of course it is better used with small to medium files but it only takes a few seconds for a 290MB file on my old 2012 iMac.

You could simply check the folder item’s modification date In activate event of the window.
No need to use FSEvents (we have FSEventsMBS class in MBS Xojo Plugins)

[quote=441835:@Christian Schmitz]You could simply check the folder item’s modification date In activate event of the window.
No need to use FSEvents (we have FSEventsMBS class in MBS Xojo Plugins)[/quote]
THANK YOU.… as I indicated that it did not need to be real time… and what I did not mention was it also did need to be Xplat

So you simply check modification date in window activate event to update window, when user comes back?

Thats the plan

@Dave S — Checking the modification date must be a good way to achieve your goal, but I am not sure it always works because it involves many subsystems in the OS. I mean, are we sure that applications using low-level file functions will behave like applications using high-level functions? I really hope so but I don’t have any useful information about that

One minor problem. It seems that an active FolderItem updates internally automatically… so in order to check if a file has been modified, the mod date needs to be cached each time the file is loaded and kept apart from the folder item

function loadfile(f)
....
modDate=f.modificationdate
end function

function hasFileChanged(f) as boolean
dim d as date =f.modificationDate
dim flag as boolean=(modDate<>d)
if flag then modDate=d
return flag
end function