Results 1 to 4 of 4

Thread: Checking directory for new files

  1. #1
    Reynard
    Guest

    Post Checking directory for new files

    How could I do that using API calls? I would just to be notified, when there are any new files in certain directory (and which files)... using API of course. Any ideas?


    Thank you very much in advance !

  2. #2
    Matthew Gates
    Guest
    Try this:


    VB Code:
    1. 'Author: Aaron Young (with a few added things from Jop)
    2. 'Origin: Vb-World.net forums
    3. 'Purpose: Monitor a Directory
    4. 'Version: VB5+
    5.  
    6.  
    7. 'You can use the FindFirstChangeNotification() and
    8. 'FindNextChangeNotification() API's to monitor a directory
    9. 'for file changes including creation of new files, i.e.
    10.  
    11.  
    12. Private Declare Function WaitForSingleObject _
    13. Lib "kernel32" (ByVal hHandle As Long, ByVal _
    14. dwMilliseconds As Long) As Long
    15.  
    16. Private Declare Function FindFirstChangeNotification _
    17. Lib "kernel32" Alias "FindFirstChangeNotificationA" _
    18. (ByVal lpPathName As String, ByVal bWatchSubtree As _
    19. Long, ByVal dwNotifyFilter As Long) As Long
    20.  
    21. Private Declare Function FindNextChangeNotification _
    22. Lib "kernel32" (ByVal hChangeHandle As Long) As Long
    23.  
    24. Private Const FILE_NOTIFY_CHANGE_FILE_NAME = &H1
    25. Private Const FILE_NOTIFY_CHANGE_LAST_WRITE As Long = &H10
    26. Private Const FILE_NOTIFY_CHANGE_CREATION As Long = &H40
    27.  
    28. Private Sub WaitForFileActivity(ByVal sDirectory As String)
    29.     Dim lHandle As Long
    30.     Dim lReturn As Long
    31.    
    32.     lHandle = FindFirstChangeNotification(sDirectory, 0&, FILE_NOTIFY_CHANGE_LAST_WRITE) 'if something's written to a file
    33.     'or FindFirstChangeNotification(sDirectory, 0&, FILE_NOTIFY_CHANGE_CREATION) 'if a file is being created
    34.     'or FindFirstChangeNotification(sDirectory, 0&, FILE_NOTIFY_CHANGE_FILE_NAME)
    35.     Do
    36.         lReturn = WaitForSingleObject(lHandle, 1)
    37.         Call FindNextChangeNotification(lHandle)
    38.         DoEvents
    39.     Loop While lReturn <> 0
    40. End Sub
    41.  
    42.  
    43. Private Sub Command1_Click()
    44.     WaitForFileActivity "C:\"
    45.     MsgBox "Change", vbSystemModal
    46. End Sub

  3. #3
    Reynard
    Guest
    Thanx Matthew, I think that's what I wanted !! I'll put that "waitforactivity" in timer (or is there any other solution...that folder is checked constantly?), I think that should work.

    Thanx again !

  4. #4
    Matthew Gates
    Guest
    No need for a timer. It's already looping, just put that in a Command Button called "Watch" or even in the Form_Load() event.

Posting Permissions

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



Click Here to Expand Forum to Full Width