Results 1 to 5 of 5

Thread: Notification of a new file

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2000
    Posts
    62
    I have an application that waits for new data to come in over the internet. Once it receives that data it writes it to a text file. I am writing a program that is going to use that text file to do some operations. My question is: Is there a way for my program to be notified that a new file has been generated? Any other suggestions will be appreciated.

    Thanks, JJK

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    You can use the FindFirstChangeNotification() and FindNextChangeNotification() API's to monitor a directory for file changes including creation of new files, i.e.

    In a Module:
    Code:
    Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
    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 FindNextChangeNotification Lib "kernel32" (ByVal hChangeHandle As Long) As Long
    
    Private Const FILE_NOTIFY_CHANGE_FILE_NAME = &H1
    
    Public Sub WaitForFileActivity(ByVal sDirectory As String)
        Dim lHandle As Long
        Dim lReturn As Long
        
        lHandle = FindFirstChangeNotification(sDirectory, 0&, FILE_NOTIFY_CHANGE_FILE_NAME)
        Do
            lReturn = WaitForSingleObject(lHandle, 1)
            Call FindNextChangeNotification(lHandle)
            DoEvents
        Loop While lReturn <> 0
    End Sub
    Example
    Code:
    Private Sub Command1_Click()
        WaitForFileActivity "C:\"
        MsgBox "Change", vbSystemModal
    End Sub

  3. #3
    Guest

    The expert has spoken :).

    But just a basic way.

    Put this code in a timer and put its interval to 100.

    Code:
    ffile = Dir("C:\file.exe")
    If Not ffile = "" Then
    'file exists
    Else
    'file hasn't been created yet
    End If

  4. #4
    New Member
    Join Date
    Aug 2000
    Location
    Landskrona, Sweden
    Posts
    7

    Question

    Aaron,
    do you know a way to call ReadDirectoryChangesW instead of FindFirstChangeNotification.
    FindFirst... only gives you a signal that something has happened but not which file that was affected.
    I think that I would get the name of the file affected by calling ReadDirectoryChangesW.
    My problem is that all examples I have found is written in C/C++ and it is NOT my favourite...

    Help?
    Magnus

    Originally posted by Aaron Young
    You can use the FindFirstChangeNotification() and FindNextChangeNotification() API's to monitor a directory for file changes including creation of new files, i.e.

    In a Module:
    Code:
    Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
    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 FindNextChangeNotification Lib "kernel32" (ByVal hChangeHandle As Long) As Long
    
    Private Const FILE_NOTIFY_CHANGE_FILE_NAME = &H1
    
    Public Sub WaitForFileActivity(ByVal sDirectory As String)
        Dim lHandle As Long
        Dim lReturn As Long
        
        lHandle = FindFirstChangeNotification(sDirectory, 0&, FILE_NOTIFY_CHANGE_FILE_NAME)
        Do
            lReturn = WaitForSingleObject(lHandle, 1)
            Call FindNextChangeNotification(lHandle)
            DoEvents
        Loop While lReturn <> 0
    End Sub
    Example
    Code:
    Private Sub Command1_Click()
        WaitForFileActivity "C:\"
        MsgBox "Change", vbSystemModal
    End Sub

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2000
    Posts
    62
    i had a problem with the code you gave me. it worked fine, but it sucked up all the memeory, very slowly. i rewrote it a little tokeep that from happening. basically, just calling the close thread api one more time. here it is:

    sDirectory = GetSetting _
    (App.Title, "Properties", "Dir", "C:\")

    'Wait for the date stamp of any file to change.
    Do
    Call FindCloseChangeNotification(lHandle)
    Call FindCloseChangeNotification(lReturn)

    lHandle = FindFirstChangeNotification(sDirectory,_
    0&, FILE_NOTIFY_CHANGE_ATTRIBUTES)

    lReturn = WaitForSingleObject(lHandle, 1)

    Call FindNextChangeNotification(lHandle)

    DoEvents

    Loop While lReturn <> 0

    Call FindCloseChangeNotification(lHandle)
    Call FindCloseChangeNotification(lReturn)

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