PDA

Click to See Complete Forum and Search --> : Notification of a new file


JJK
Aug 9th, 2000, 05:07 PM
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

Aaron Young
Aug 10th, 2000, 09:11 PM
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: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 SubExamplePrivate Sub Command1_Click()
WaitForFileActivity "C:\"
MsgBox "Change", vbSystemModal
End Sub

Aug 10th, 2000, 09:34 PM
But just a basic way.

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

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

magnus.karlsson
Aug 18th, 2000, 08:50 AM
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: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 SubExamplePrivate Sub Command1_Click()
WaitForFileActivity "C:\"
MsgBox "Change", vbSystemModal
End Sub

JJK
Aug 18th, 2000, 09:12 AM
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)