Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check for file changes in directory

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I am trying to check a directory for either a new file added or when a change has been made to current file. I am not sure how to start writing the code for this. Any help would be appreciated. I need to know the name of the file that has been changed so I can extract information from that file.
Thanks in advance
John
 
Here's a minor alternative to John's solution which avoids the need to do a folder comparison all the time. You only need to do so when you know a file has changed. Naturally it'sa little more complicated.

Firstly you'll need a class module. Call it FileNotify. The drop in this code (I've included some extra declarations that should make it easier to extend the class's functionality). Be warned that this example is illustrative rather than exhaustive (and it has its own drawbacks)
[tt]
Option Explicit

Private Declare Function FindFirstChangeNotification Lib "kernel32" Alias "FindFirstChangeNotificationA" (ByVal lpPathName As String, ByVal bWatchSubtree As Long, ByVal dwNotifyFilter As Long) As Long
Private Const FILE_NOTIFY_CHANGE_ATTRIBUTES = &H4
Private Const FILE_NOTIFY_CHANGE_DIR_NAME = &H2
Private Const FILE_NOTIFY_CHANGE_FILE_NAME = &H1
Private Const FILE_NOTIFY_CHANGE_LAST_WRITE = &H10
Private Const FILE_NOTIFY_CHANGE_SECURITY = &H100
Private Const FILE_NOTIFY_CHANGE_SIZE = &H8
Private Const INVALID_HANDLE_VALUE = -1&
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 Const WAIT_ABANDONED = &H80
Private Const WAIT_FAILED = &HFFFFFFFF
Private Const WAIT_OBJECT_0 = &H0
Private Const WAIT_TIMEOUT = &H102
Private Const INFINITE = &HFFFF
Public Event FileChange()


Public Sub Watch(strFolder As String)
Dim lresult As Long

lresult = FindFirstChangeNotification(strFolder, False, FILE_NOTIFY_CHANGE_FILE_NAME Or FILE_NOTIFY_CHANGE_LAST_WRITE)
If lresult <> INVALID_HANDLE_VALUE Then WaitLoop lresult
End Sub

Private Sub WaitLoop(hNotify)
Dim lresult As Long

lresult = -1
Do Until lresult = WAIT_OBJECT_0
lresult = WaitForSingleObject(hNotify, 1000)
DoEvents
Loop
RaiseEvent FileChange
End Sub
[/tt]
Next drop put a command button on a form, and drop in the following code:
[[tt]
Option Explicit
Private WithEvents Notifier As Filenotify

Private Sub Command1_Click()
Set Notifier = New Filenotify
Notifier.Watch (&quot;c:\temp&quot;) ' Put the name of the folder you want to watch here
End Sub

Private Sub notifier_filechange()
MsgBox &quot;Oh, a file has changed...&quot;
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top