dcsimg
Results 1 to 20 of 20

Thread: ReadDirectoryChanges problem

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2019
    Posts
    20

    ReadDirectoryChanges problem

    I have downloaded a sample from the internet on how to code ReadDirectoryChanges to be able to detect modified files like text files, word files, excel files, jpg files.

    I was able to detect modifications made to text files immediately upon saving but not with excel files which is takes 10 times pressing save button and it did not detect for word files upon any saving mode. With jpg files, the save location is coming from the temp folder instead from the folder where it was opened.

    I am doing this on Windows 7 Pro sp1.

    Any suggestions ? I need to detect the opened file and if it modified and the full path of the file.
    Last edited by techbee; Sep 10th, 2019 at 12:17 AM.

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    3,553

    Re: ReadDirectoryChanges problem

    How is this different from your previous thread?
    Which you marked as resolved by the way...

    http://www.vbforums.com/showthread.p...d-get-its-path

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2019
    Posts
    20

    Re: ReadDirectoryChanges problem

    I marked that resolved about finding a code but not about working code. Now that I have tested it, it has problems that I've mentioned.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,438

    Re: ReadDirectoryChanges problem

    word and excel create temporary files and may have to delete the temporary files before releasing the original file, or may even copy the temporary files over the originals

    as we can not see the exact code as you are using hard to guess if any issues there

    when moving one program from xp to w 10 i had serious timing problems that i had to resolve for reading the newly created files, after being detected by ReadDirectoryChangesW
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5
    Frenzied Member gibra's Avatar
    Join Date
    Oct 2009
    Location
    ITALY
    Posts
    1,659

    Re: ReadDirectoryChanges problem

    See this article:
    Macro Code to Check Whether a File Is Already Open
    https://web.archive.org/web/20150113....com/kb/138621

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Feb 2019
    Posts
    20

    Re: ReadDirectoryChanges problem

    Quote Originally Posted by westconn1 View Post
    word and excel create temporary files and may have to delete the temporary files before releasing the original file, or may even copy the temporary files over the originals

    as we can not see the exact code as you are using hard to guess if any issues there

    when moving one program from xp to w 10 i had serious timing problems that i had to resolve for reading the newly created files, after being detected by ReadDirectoryChangesW
    Westconn1, attached is the project file that I am using. It has all the codes there. I have downloaded the original file from internet. Hope you can check it out and assist me on the problem.
    Attached Files Attached Files

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Feb 2019
    Posts
    20

    Re: ReadDirectoryChanges problem

    Been trying to resolve this issue myself but still can find solution and I have posted the code I used for some days now but I haven't heard from anybody.

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,438

    Re: ReadDirectoryChanges problem

    I have posted the code I used for some days now
    missed it or i could have had a look at the weekend
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Feb 2019
    Posts
    20

    Re: ReadDirectoryChanges problem

    Quote Originally Posted by westconn1 View Post
    missed it or i could have had a look at the weekend
    Hoping for your update or from any members. TIA

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Feb 2019
    Posts
    20

    Re: ReadDirectoryChanges problem

    Quote Originally Posted by westconn1 View Post
    missed it or i could have had a look at the weekend
    Hoping for your update or from any members. TIA

  11. #11
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    3,553

    Re: ReadDirectoryChanges problem

    Check this other thread, it's dealing about the same topic.
    But no code solution either.

    http://www.vbforums.com/showthread.p...stamp-changes*

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,438

    Re: ReadDirectoryChanges problem

    after doing a bit of testing i don't think this is going to do what you want

    files that are opened but not changed are not picked up even with FILE_NOTIFY_CHANGE_LAST_ACCESS
    most other files are not picked up until the file is closed, or later with some office files, so you can not use this to detect which files are currently open

    you could create a log file or something, but i doubt that is relevant to what you want to do

    i would possibly use the snapshot to work with a list of open processes then get the open files from that, either using a timer or some event that may be able to trigger the code

    readdirectorychangesW works great for what i use it, but i am not sure for your application
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Feb 2019
    Posts
    20

    Re: ReadDirectoryChanges problem

    Westconn1, I am needing to detect files that is opened and modified later. So i believe that this code is right problem is i cant detect modifications on ms word file immediately after saved unlike text files.

  14. #14
    PowerPoster
    Join Date
    Jun 2013
    Posts
    4,345

    Re: ReadDirectoryChanges problem

    To determine, whether ReadDirectoryChangesW is the right API for your use-case,
    you could use the RC5-implementation for a short test, whether the approach is suitable at all for your purposes:

    Code:
    Option Explicit
    
    Private WithEvents DW As cDirList
     
    Private Sub Form_Load()
      Set DW = New_c.FSO.GetDirList("C:")
          DW.DirWatchStart
    End Sub
    
    Private  Sub DW_DirWatchActionOn(ByVal WatchDir As String, RelFileOrDirName As  String, ByVal Action As vbRichClient5.FILE_ACTION, ActionText As String,  ByVal WatchingSubTree As Boolean, ByVal WatchingFilter As  vbRichClient5.FILE_NOTIFY_CHANGE)
      'don't react to changes on Temp-Directories
      If InStr(LCase$(RelFileOrDirName), "\temp\") Then Exit Sub
      If InStr(LCase$(RelFileOrDirName), "\tmp\") Then Exit Sub
      
      'all other changes on C: will be reported (according to certain File-Endings)
      Select Case LCase$(New_c.FSO.GetFileExtension(RelFileOrDirName))
        Case "doc", "docx", "xls", "xlsx" '<- define the File-Endings you watch out for
          Debug.Print ActionText, RelFileOrDirName
      End Select
    End Sub
    HTH

    Olaf

  15. #15
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,438

    Re: ReadDirectoryChanges problem

    @ olaf
    now that has got me interested
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Feb 2019
    Posts
    20

    Re: ReadDirectoryChanges problem

    Schmidt, it is somehow the same with ReadDirectoryChangesW in relation to my problem with MS Word file that when I save the document, it doesn't detect changes from the file. It can only detect change on the file when the MS word document is being closed. And another problem with the ms word document file is it only reads the temporary file created and not the actual file like the actual file is situation.doc and the name read is ~$uation.doc
    Last edited by techbee; Yesterday at 11:05 PM.

  17. #17
    Frenzied Member gibra's Avatar
    Join Date
    Oct 2009
    Location
    ITALY
    Posts
    1,659

    Re: ReadDirectoryChanges problem

    @techbee

    I've you read my post #5 ?
    ths solution come directly from Microsoft.
    http://www.vbforums.com/showthread.p...=1#post5415047

  18. #18
    PowerPoster
    Join Date
    Jun 2013
    Posts
    4,345

    Re: ReadDirectoryChanges problem

    Quote Originally Posted by techbee View Post
    ... it only reads the temporary file created and not the actual file like the actual file is situation.doc
    and the name read is ~$uation.doc
    I've already taken a look at the VB6-Example-Code you've posted in a Zip.
    It's not well-written IMO - (will cause instabilities due to the CreateThread-API-usage - and it will skip FileChange-Events).

    If you want to see what the ReadDirectoryChangesW-API can (really) do,
    you should try to find another (proper) VB6-implementation (which does not skip FileEvents).

    And as said, for a fast tests you could try to use the one which is contained in RC5
    (just install it, and then run the code-snippet I've already posted to see how the "lifecycle of your Office-File from Opening to Saving" gets reported).

    I get FileEvents like:
    - "creation": of ~$uation.doc
    - followed by "change", "creation", "rename" sequences in case you save the document
    - followed by "delete" of ~$uation.doc when you close the document-instance

    The real FileName (in your case "situation.doc") in the above mentioned "save-sequence of immediately following Events" -
    will come up only later in the sequence - in the "rename" Event (or in one, following the rename-event - don't remember exactly which type, I've did that test yesterday on a different machine and don't have Office here on this one).

    Olaf

  19. #19
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,788

    Re: ReadDirectoryChanges problem

    There is another method for watching a folder for changes, it uses the API SHChangeNotifyRegister.
    There is a sample similar to your test program in post #9.

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Feb 2019
    Posts
    20

    Re: ReadDirectoryChanges problem

    Olaf, only in excel file have that sequence you mentioned but not only doc files.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width