Solved

New file "event"

Posted on 1998-12-06
3
166 Views
Last Modified: 2010-05-03
I want to get an "event" then a new file/update to file occurs in a directory. How can I do this?
0
Comment
Question by:manne
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 
LVL 2

Accepted Solution

by:
dm_14 earned 100 total points
ID: 1448433
1. Use the FindNextChangeNotification and FindFirstChangeNotification
2. You can find the declarations and constants in the VB5 text viewer
3. More info available on Microsofts MSDN site
4. Here is a sample piece of code:

Private Sub Command1_Click()
Do
    Dim lDir As Long, lFound1 As Long, lFound2 As Long
    sPath = Left$(sSourceDir, Len(sSourceDir) - 1)
    lDir = FindFirstChangeNotification(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
         DoEvents
         ' -- ** Place your event code here ** --
         MsgBox "The watched directory has been changed.  Resuming watch..."
     End If
   Loop
end sub
0
 
LVL 2

Expert Comment

by:dm_14
ID: 1448434
Correction ..
2. You can find the declarations and constants in the VB5  API text viewer
0
 
LVL 2

Expert Comment

by:dm_14
ID: 1448435
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_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

Public Declare Function FindFirstChangeNotification Lib "kernel32" _
    Alias "FindFirstChangeNotificationA" _
   (ByVal lpPathName As String, _
    ByVal bWatchSubtree As Long, _
    ByVal dwNotifyFilter As Long) As Long

Public Declare Function FindCloseChangeNotification 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 = FindFirstChangeNotification(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 = FindCloseChangeNotification(lDir)
   
Loop
End Sub

0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question