Nathaniel Zhu admin Posts : 3469  |
Posted 20/07/2007 06:18:59 PM | | Form:
Private Sub Form_Load()
lbMsg = "Press 'Begin Watch'"
End Sub
Private Sub cmdEnd_Click()
If hWatched > 0 Then Call WatchDelete(hWatched)
Unload Me
Set Form1 = Nothing
End Sub
Private Sub cmdStop_Click()
'clean up by deleting the handle to the watched directory
Call WatchDelete(hWatched)
hWatched = 0
cmdBegin.Enabled = True
lbMsg = "Press 'Begin Watch'"
End Sub
Private Sub cmdBegin_Click()
Dim r As Long
Dim watchPath As String
Dim watchStatus As Long
watchPath = "c:\temp"
terminateFlag = False
cmdBegin.Enabled = False
lbMsg = "Using Explorer and Notepad, create, modify, rename, delete or "
lbMsg = lbMsg & "change the attributes of a text file in the watched directory."""
'get the first file text attributes to the listbox (if any)
WatchChangeAction watchPath
'show a msgbox to indicate the watch is starting
MsgBox "Beginning watching of folder " & watchPath & " .. press OK"
'create a watched directory
hWatched = WatchCreate(watchPath, FILE_NOTIFY_FLAGS)
'poll the watched folder
watchStatus = WatchDirectory(hWatched, 100)
'if WatchDirectory exited with watchStatus = 0,
'then there was a change in the folder.
If watchStatus = 0 Then
'update the listbox for the first file found in the
'folder and indicate a change took place.
WatchChangeAction watchPath
MsgBox "The watched directory has been changed. Resuming watch..."
'(perform actions)
'this is where you'd actually put code to perform a
'task based on the folder changing.
'FindNextChangeNotification API, again exiting if
'watchStatus indicates the terminate flag was set
Do
watchStatus = WatchResume(hWatched, 100)
If watchStatus = -1 Then
'watchStatus must have exited with the terminate flag
MsgBox "Watching has been terminated for " & watchPath
Else: WatchChangeAction watchPath
MsgBox "The watched directory has been changed again."
'(perform actions)
'this is where you'd actually put code to perform a
'task based on the folder changing.
End If
Loop While watchStatus = 0
Else
'watchStatus must have exited with the terminate flag
MsgBox "Watching has been terminated for " & watchPath
End If
End Sub
Private Function WatchCreate(lpPathName As String, flags As Long) As Long
'FindFirstChangeNotification members:
'
' lpPathName: folder to watch
' bWatchSubtree:
' True = watch specified folder and its sub folders
' False = watch the specified folder only
' flags: OR'd combination of the FILE_NOTIFY_ flags to apply
WatchCreate = FindFirstChangeNotification(lpPathName, False, flags)
End Function
Private Sub WatchDelete(hWatched As Long)
Dim r As Long
terminateFlag = True
DoEvents
r = FindCloseChangeNotification(hWatched)
End Sub
Private Function WatchDirectory(hWatched As Long, interval As Long) As Long
'Poll the watched folder.
'The Do..Loop will exit when:
' r = 0, indicating a change has occurred
' terminateFlag = True, set by the WatchDelete routine
Dim r As Long
Do
r = WaitForSingleObject(hWatched, interval)
DoEvents
Loop While r <> 0 And terminateFlag = False
WatchDirectory = r
End Function
Private Function WatchResume(hWatched As Long, interval) As Boolean
Dim r As Long
r = FindNextChangeNotification(hWatched)
Do
r = WaitForSingleObject(hWatched, interval)
DoEvents
Loop While r <> 0 And terminateFlag = False
WatchResume = r
End Function
Private Sub WatchChangeAction(fPath As String)
Dim fName As String
List1.Clear
fName = Dir(fPath & "\" & "*.txt")
If fName > "" Then
List1.AddItem "path: " & vbTab & fPath
List1.AddItem "file: " & vbTab & fName
List1.AddItem "size: " & vbTab & FileLen(fPath & "\" & fName)
List1.AddItem "attr: " & vbTab & GetAttr(fPath & "\" & fName)
End If
End Sub
Private Sub lbMsg_Click()
End Sub
Module:
Option Explicit
Public Const INFINITE = &HFFFF
Public Const FILE_NOTIFY_CHANGE_FILE_NAME As Long = &H1
Public Const FILE_NOTIFY_CHANGE_DIR_NAME As Long = &H2
Public Const FILE_NOTIFY_CHANGE_ATTRIBUTES As Long = &H4
Public Const FILE_NOTIFY_CHANGE_SIZE As Long = &H8
Public Const FILE_NOTIFY_CHANGE_LAST_WRITE As Long = &H10
Public Const FILE_NOTIFY_CHANGE_LAST_ACCESS As Long = &H20
Public Const FILE_NOTIFY_CHANGE_CREATION As Long = &H40
Public Const FILE_NOTIFY_CHANGE_SECURITY As Long = &H100
Public Const FILE_NOTIFY_FLAGS = FILE_NOTIFY_CHANGE_ATTRIBUTES Or _
FILE_NOTIFY_CHANGE_FILE_NAME Or _
FILE_NOTIFY_CHANGE_LAST_WRITE
Declare Function FindFirstChangeNotification Lib "kernel32" _
Alias "FindFirstChangeNotificationA" _
(ByVal lpPathName As String, _
ByVal bWatchSubtree As Long, _
ByVal dwNotifyFilter As Long) As Long
Declare Function FindCloseChangeNotification Lib "kernel32" _
(ByVal hChangeHandle As Long) As Long
Declare Function FindNextChangeNotification Lib "kernel32" _
(ByVal hChangeHandle As Long) As Long
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
enjoy!!!!!!!
|