Results 1 to 4 of 4

Thread: Responding to directory events

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2000
    Posts
    12
    What is the most resource efficient method of a VB application responding to a new file being written to a directory (other than creating an NT service)?

    Thanks

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    If you're not afraid of timers, I have a simple solution,.

  3. #3
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    You can use the FindFirstChangeNotification API, ie.
    Code:
    Private Declare Function FindFirstChangeNotification Lib "kernel32" Alias "FindFirstChangeNotificationA" (ByVal lpPathName As String, ByVal bWatchSubtree As Long, ByVal dwNotifyFilter As Long) As Long
    Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
    
    Private Const FILE_NOTIFY_CHANGE_FILE_NAME = &H1
    
    Private Sub Form_Activate()
        Dim lWait As Long
        
        lWait = 1
        While lWait
            lWait = FindFirstChangeNotification("C:\Files", True, FILE_NOTIFY_CHANGE_FILE_NAME)
            DoEvents
            If WaitForSingleObject(lWait, 100) = 0 Then
                Text1.SelText = "Something Changed." & vbCrLf
            End If
        Wend
    End Sub
    [Edited by Aaron Young on 04-03-2000 at 02:53 PM]

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    This will return all new files in a variant containing an array:
    Code:
    Function SearchDIR(directory)
        
        On Error Resume Next 'oldfiles will cause subscript out of range the first time.
        Static oldfiles() As String 'This static remains in memory after first function call
        Dim newfiles() As String: ReDim newfiles(0) 'Temporary holds the new ones
        Dim foundfiles() As String: ReDim foundfiles(0) 'The array to send in return
        
        fil = Dir(directory, 7) 'Catch first file (hidden/system/archive files readen)
        Do Until fil = ""
            ReDim Preserve newfiles(UBound(newfiles) + 1) 'Resize array
            newfiles(UBound(newfiles) - 1) = fil
            For n = 0 To UBound(oldfiles) 'Search the old ones...
                If fil = oldfiles(n) Then ' ... to test if fil is old
                    Exit For
                End If
                If n = UBound(oldfiles) Then '... if no matches, add to foundlist
                    ReDim Preserve foundfiles(UBound(foundfiles) + 1) 'Resize array
                    foundfiles(UBound(foundfiles) - 1) = fil 'Add
                End If
            Next n
            fil = Dir() 'Find next file
        Loop
        
        ReDim oldfiles(UBound(newfiles) - 1) 'Removing last empty item in each array
        ReDim Preserve newfiles(UBound(newfiles) - 1)
        ReDim Preserve foundfiles(UBound(foundfiles) - 1)
        For n = 0 To UBound(newfiles) ' Store filenames until next search
            oldfiles(n) = newfiles(n)
        Next n
        SearchDIR = foundfiles() 'Return the found files in array
    
    End Function
    Calling this function the first time wont return anything. Calling it next time will return all new files that are added since then. Hope you get things work!

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