pythondocxfile-monitoring

Getting issue while monitoring the .docx file changes


I am trying to monitor the .docx file with the python code as follows

    hDir = win32file.CreateFile (
  dirPath,
  FILE_LIST_DIRECTORY,
  win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
  None,
  win32con.OPEN_EXISTING,
  win32con.FILE_FLAG_BACKUP_SEMANTICS,
  None
)

while 1:
    # Wait for a change to occur
    results = win32file.ReadDirectoryChangesW (
                                               hDir,
                                               1024,
                                               False,
                                               win32con.FILE_NOTIFY_CHANGE_LAST_WRITE,
                                               None,
                                               None
                                               )

When I am trying to capture the changes made to the file, filename is coming with the temporary file names of ms word document. How to capture these temporary file changes in python.


Solution

  • This works with the following code. If we can monitor the FILE_NOTIFY_CHANGE_SECURITY event then we can able to track all the temporary file changes also.

     readFlags = win32con.FILE_NOTIFY_CHANGE_FILE_NAME  | \
            win32con.FILE_NOTIFY_CHANGE_DIR_NAME   | \
            win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES | \
            win32con.FILE_NOTIFY_CHANGE_SIZE       | \
            win32con.FILE_NOTIFY_CHANGE_LAST_WRITE | \
            win32con.FILE_NOTIFY_CHANGE_SECURITY
    # Wait for new data and call ProcessNewData for each new chunk that's written
    while 1:
        # Wait for a change to occur
        results = win32file.ReadDirectoryChangesW (
                                                   hDir,
                                                   1024,
                                                   False,
                                                   readFlags,
                                                   None
                                                   )