I've made a "folder watcher" using APIs that would monitor folder(s) for changes (actually I think I got most of the code from some web site)...you'll notice it uses a Timer control--not all that efficient, but much less of a performance hit than a Do...While loop.
Hope this is useful.
Below is a sample (check the MSDN for more information on the APIs)
'!! ================================================================================================
'-- Place the following code in a form which has a Timer control named "tmrCheckFolder"
'!! ================================================================================================
Option Explicit
Private Enum FILE_NOTIFY_CHANGE
FNC_FILE_NAME = &H1
FNC_DIR_NAME = &H2
' FNC_ATTRIBUTES = &H4
' FNC_SIZE = &H8
' FNC_LAST_WRITE = &H10
' FNC_SECURITY = &H100
End Enum
Private Declare Function FindFirstChangeNotification Lib "kernel32" Alias "FindFirstChangeNotificationA" (ByVal lpPathName As String, ByVal bWatchSubtree As Long, ByVal dwNotifyFilter As Byte) As Long
Private Declare Function FindNextChangeNotification Lib "kernel32" (ByVal hChangeHandle As Long) As Long
Private Declare Function FindCloseChangeNotification Lib "kernel32" (ByVal hChangeHandle As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function GetLastError Lib "kernel32" () As Long
Private mlngHandle As Long
Private mstrFolder As String
Private Const WAIT_OBJECT_0 As Long = 0
Private Const INVALID_HANDLE_VALUE As Long = -1
'
Private Sub Form_Load()
'===================================================================================================
mstrFolder = "C:\Temp"
'-- When this is true, changes in subdirectories will also trigger "WaitForSingleObject"
Const cblnCHECK_RECURSIVELY As Boolean = False
'!! I know you won't believe me, but without this Debug.Print statement it wouldn't work in a compiled version.
Debug.Print "Watching folder: " & mstrFolder & IIf(cblnCHECK_RECURSIVELY, vbCrLf & vbCrLf & "Recursively", vbNullString)
mlngHandle = FindFirstChangeNotification(mstrFolder, cblnCHECK_RECURSIVELY, FNC_FILE_NAME Or FNC_DIR_NAME)
If mlngHandle = -1 Then
MsgBox "Failed on: " & mstrFolder & vbCrLf & vbCrLf & "Error number: " & GetLastError(), vbExclamation
Unload Me
Else
With Me.tmrCheckFolder
.Interval = 250
.Enabled = True
End With
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
'===================================================================================================
If mlngHandle <> INVALID_HANDLE_VALUE Then Call FindCloseChangeNotification(mlngHandle)
End Sub
Private Sub tmrCheckFolder_Timer()
'===================================================================================================
Call CheckFolder
End Sub
Private Sub CheckFolder()
'===================================================================================================
If WaitForSingleObject(mlngHandle, 100) = WAIT_OBJECT_0 Then
Debug.Print "Change occurred in: " & mstrFolder
Call FindNextChangeNotification(mlngHandle)
End If
End Sub