manne
asked on
New file "event"
I want to get an "event" then a new file/update to file occurs in a directory. How can I do this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry .. The code I sent has a few errors
Here is a better example :
1. Here are the declarations you need :
(Some of the constants allow you to detect different changes to a file)
Public Const INFINITE = &HFFFF
Public Const FILE_NOTIFY_CHANGE_FILE_NA ME As Long = &H1
Public Const FILE_NOTIFY_CHANGE_DIR_NAM E As Long = &H2
Public Const FILE_NOTIFY_CHANGE_ATTRIBU TES As Long = &H4
Public Const FILE_NOTIFY_CHANGE_SIZE As Long = &H8
Public Const FILE_NOTIFY_CHANGE_LAST_WR ITE As Long = &H10
Public Const FILE_NOTIFY_CHANGE_LAST_AC CESS As Long = &H20
Public Const FILE_NOTIFY_CHANGE_CREATIO N As Long = &H40
Public Const FILE_NOTIFY_CHANGE_SECURIT Y As Long = &H100
Public Const FILE_NOTIFY_FLAGS = FILE_NOTIFY_CHANGE_ATTRIBU TES Or _
FILE_NOTIFY_CHANGE_FILE_NA ME Or _
FILE_NOTIFY_CHANGE_LAST_WR ITE
Public Declare Function FindFirstChangeNotificatio n Lib "kernel32" _
Alias "FindFirstChangeNotificati onA" _
(ByVal lpPathName As String, _
ByVal bWatchSubtree As Long, _
ByVal dwNotifyFilter As Long) As Long
Public Declare Function FindCloseChangeNotificatio n Lib "kernel32" _
(ByVal hChangeHandle As Long) As Long
Public Declare Function FindNextChangeNotification Lib "kernel32" _
(ByVal hChangeHandle As Long) As Long
Public Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Public Const WAIT_OBJECT_0 = &H0
Public Const WAIT_ABANDONED = &H80
Public Const WAIT_IO_COMPLETION = &HC0
Public Const WAIT_TIMEOUT = &H102
Public Const STATUS_PENDING = &H103
2. Here is the code to place in a command button :
Private Sub Command1_Click()
Do
Dim lDir As Long, lFound1 As Long, lFound2 As Long, sPath As String
sPath = "c:\temp1" ' *** directory to watch ***
lDir = FindFirstChangeNotificatio n(sPath, False, FILE_NOTIFY_FLAGS)
Do
lFound1 = WaitForSingleObject(lDir, 100)
DoEvents: If bFlag = True Then Exit Do
Loop While lFound1 <> 0
Do
lFound2 = FindNextChangeNotification (lDir)
Do
lFound2 = WaitForSingleObject(lDir, 100)
DoEvents
If bFlag = True Then Exit Do
Loop While lFound2 <> 0
If bFlag = True Then Exit Do
If iFound2 = 0 Then
MsgBox sPath
MsgBox "The watched directory has been changed. Resuming watch..."
End If
Loop
lFound2 = FindCloseChangeNotificatio n(lDir)
Loop
End Sub
Here is a better example :
1. Here are the declarations you need :
(Some of the constants allow you to detect different changes to a file)
Public Const INFINITE = &HFFFF
Public Const FILE_NOTIFY_CHANGE_FILE_NA
Public Const FILE_NOTIFY_CHANGE_DIR_NAM
Public Const FILE_NOTIFY_CHANGE_ATTRIBU
Public Const FILE_NOTIFY_CHANGE_SIZE As Long = &H8
Public Const FILE_NOTIFY_CHANGE_LAST_WR
Public Const FILE_NOTIFY_CHANGE_LAST_AC
Public Const FILE_NOTIFY_CHANGE_CREATIO
Public Const FILE_NOTIFY_CHANGE_SECURIT
Public Const FILE_NOTIFY_FLAGS = FILE_NOTIFY_CHANGE_ATTRIBU
FILE_NOTIFY_CHANGE_FILE_NA
FILE_NOTIFY_CHANGE_LAST_WR
Public Declare Function FindFirstChangeNotificatio
Alias "FindFirstChangeNotificati
(ByVal lpPathName As String, _
ByVal bWatchSubtree As Long, _
ByVal dwNotifyFilter As Long) As Long
Public Declare Function FindCloseChangeNotificatio
(ByVal hChangeHandle As Long) As Long
Public Declare Function FindNextChangeNotification
(ByVal hChangeHandle As Long) As Long
Public Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Public Const WAIT_OBJECT_0 = &H0
Public Const WAIT_ABANDONED = &H80
Public Const WAIT_IO_COMPLETION = &HC0
Public Const WAIT_TIMEOUT = &H102
Public Const STATUS_PENDING = &H103
2. Here is the code to place in a command button :
Private Sub Command1_Click()
Do
Dim lDir As Long, lFound1 As Long, lFound2 As Long, sPath As String
sPath = "c:\temp1" ' *** directory to watch ***
lDir = FindFirstChangeNotificatio
Do
lFound1 = WaitForSingleObject(lDir, 100)
DoEvents: If bFlag = True Then Exit Do
Loop While lFound1 <> 0
Do
lFound2 = FindNextChangeNotification
Do
lFound2 = WaitForSingleObject(lDir, 100)
DoEvents
If bFlag = True Then Exit Do
Loop While lFound2 <> 0
If bFlag = True Then Exit Do
If iFound2 = 0 Then
MsgBox sPath
MsgBox "The watched directory has been changed. Resuming watch..."
End If
Loop
lFound2 = FindCloseChangeNotificatio
Loop
End Sub
2. You can find the declarations and constants in the VB5 API text viewer