0

Is there a way ( or an API ) to know when a text file is edited ( by a program or by a person ) and do a specific action ?

For example: I want to show a MessageBox when the file c:\Users\john\free.txt is edited.

xRobot
  • 25,579
  • 69
  • 184
  • 304
  • You could always check the modified timestamp, and see if it changes to something newer. – swalog Apr 18 '12 at 10:20
  • There is no API that would directly give you that information, see this question: http://stackoverflow.com/questions/1990535/win32-files-locked-for-reading-how-to-find-out-whos-locking-them. And, btw, there is no difference whether program or person is modifying a file - a person needs to use some program, isn't it? :) – Bojan Komazec Apr 18 '12 at 10:25
  • Look at the answers of http://stackoverflow.com/questions/3517460/is-there-anything-like-inotify-on-windows – jofel Apr 18 '12 at 10:28
  • 3
    @Bojan, yes there is. Use FindFirstChangeNotification and ReadDirectoryChanges. – Patrick Apr 18 '12 at 10:29
  • @Patrick Thanks. There was a question earlier today about Microsoft API which notifies on registry changes (`RegNotifyChangeKeyValue`) and after I posted my comment here I was thinking why some similar API wouldn't exist for the file system. And here it is! (+1 for your answer) – Bojan Komazec Apr 18 '12 at 10:41

4 Answers4

5

Depends on when you exactly want to know it.

  • is your application running continuously and do you want to see any change as soon as possible?
  • is your application a simple command-line application that needs to check for changes once?

In the second case, you could check the modification dates of the file (as suggested by PoweRoy and Michal) or use a hash (as suggested by PoweRoy).

If your application is running continuously, you should use the FindFirstChangeNotification and ReadDirectoryChanges functions. You can read more about it on the following pages:

Patrick
  • 23,217
  • 12
  • 67
  • 130
  • 1
    Combining the hash approach with the change notification would be an even better approach for performance and security issues – Adel Boutros Apr 18 '12 at 10:30
1

Simplest: compare modification dates. But this can be manipulated.

Or make a hash of the original file and compare it with the current file.

RvdK
  • 19,580
  • 4
  • 64
  • 107
0

GetFileTime should help you.

http://msdn.microsoft.com/en-us/library/ms724320%28v=vs.85%29.aspx

and there is GetFileAttributesEx as well.

CyberGuy
  • 2,783
  • 1
  • 21
  • 31
0

check the file's last modify datetime. This method retrieves status information related to a given CFile object instance or a given file path.

BOOL GetStatus( CFileStatus& rStatus ) const; static BOOL PASCAL GetStatus( LPCTSTR lpszFileName, CFileStatus& rStatus );

Parameters rStatus A reference to a user-supplied CFileStatus structure that will receive the status information. The CFileStatus structure has the following fields:

CTime m_ctime The date and time the file was created.

CTime m_mtime The date and time the file was last modified.

CTime m_atime The date and time the file was last accessed for reading.

ULONGLONG m_size The logical size of the file in bytes, as reported by the DIR command.

BYTE m_attribute The attribute byte of the file.

char m_szFullName[_MAX_PATH] The absolute filename in the Windows character set.

lpszFileName A string in the Windows character set that is the path to the desired file. The path can be relative or absolute, or it can contain a network path name.

Return Value TRUE if the status information for the specified file is successfully obtained; otherwise, FALSE. PS:information from MSDN