Results 1 to 4 of 4

Thread: SubFolder Change Notification.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2008
    Posts
    84

    SubFolder Change Notification.

    Hello pplz,

    i have to complete a simple task to detect and new folder create or deleted withing a given Folder.

    i tried it with FileSystemObject but its is very slow even running on, 1 sec interval timer!

    i need some method to detect changes through API, i came are API's like

    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    can someone help VB6 equivalent of codings?

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: SubFolder Change Notification.

    This might give you a start
    Code:
    Option Explicit
    
    Private Const FILE_NOTIFY_CHANGE_DIR_NAME As Long = 2
    Private Const FILE_FLAG_BACKUP_SEMANTICS As Long = &H2000000
    Private Const FILE_SHARE_READ As Long = &H1
    Private Const FILE_SHARE_WRITE As Long = &H2
    Private Const OPEN_EXISTING As Long = 3
    Private Const GENERIC_READ As Long = &H80000000
    Private Const INVALID_HANDLE_VALUE As Long = -1
    Private Const MAX_PATH As Long = 260
    Private Const FILE_ACTION_ADDED As Long = 1
    Private Const FILE_ACTION_REMOVED As Long = 2
    
    Private Type FILE_NOTIFY_INFORMATION
        NextEntryOffset As Long
        Action As Long
        FilenameLength As Long
        FileName(MAX_PATH - 1) As Byte
    End Type
    
    Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
                    (ByVal lpFileName As String, _
                     ByVal dwDesiredAccess As Long, _
                     ByVal dwShareMode As Long, _
                     ByVal lpSecurityAttributes As Long, _
                     ByVal dwCreationDisposition As Long, _
                     ByVal dwFlagsAndAttributes As Long, _
                     ByVal hTemplateFile As Long) As Long
                     
                     
    Private Declare Function ReadDirectoryChangesW Lib "kernel32" _
                    (ByVal hDirectory As Long, _
                     ByVal lpbufferout As Long, _
                     ByVal nBufferLength As Long, _
                     ByVal bwatchSubtree As Long, _
                     ByVal dwNotifyFilter As Long, _
                     lpBytesReturned As Long, _
                     ByVal lpOverlapped As Long, _
                     ByVal lpCompletionRoutine As Long) As Long
                     
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
                    (Destination As Any, _
                     Source As Any, _
                     ByVal Length As Long)
    
    Private Declare Function CloseHandle Lib "kernel32" _
                    (ByVal hObject As Long) As Long
    
    Private Sub cmdStart_Click()
    Dim lngHandle As Long
    Dim lngReturn As Long
    Dim intI As Integer
    Dim bytRet(280) As Byte
    Dim lngLen As Long
    Dim strFolder As String
    Dim udtFNI As FILE_NOTIFY_INFORMATION
    '
    ' Get a Handle to the Directory we want to monitor
    '
    lngHandle = CreateFile("c:\Doogle\Test\", GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0&)
    If lngHandle <> INVALID_HANDLE_VALUE Then
        '
        ' Wait for a Change
        '
        lngReturn = ReadDirectoryChangesW(lngHandle, VarPtr(bytRet(0)), 281&, 0&, FILE_NOTIFY_CHANGE_DIR_NAME, lngLen, 0&, 0&)
        '
        ' Establish the resulting UDT
        '
        CopyMemory udtFNI, bytRet(0), lngLen
        '
        ' Convert the Filename to a string
        '
        For intI = 0 To UBound(udtFNI.FileName)
            strFolder = strFolder & Chr(udtFNI.FileName(intI))
        Next intI
        strFolder = StrConv(strFolder, vbFromUnicode)
        '
        ' Check whether it's a new folder or
        ' one that's been deleted
        ' And output the details to the Immediate Window
        '
        Select Case udtFNI.Action
            Case FILE_ACTION_ADDED
                Debug.Print "Added: ";
            Case FILE_ACTION_REMOVED
                Debug.Print "Removed: ";
        End Select
        Debug.Print strFolder
        '
        ' Tidy up
        '
        lngReturn = CloseHandle(lngHandle)
    End If
    End Sub

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2008
    Posts
    84

    Re: SubFolder Change Notification.

    but this process is not async as i guess, and i have to keep processing other stuff's along with this ?

    so what are the possibilities and i am not much familiar with API's

  4. #4
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: SubFolder Change Notification.

    I 'improved' the example - take a look at this thread: http://www.vbforums.com/showthread.php?t=649240 (Post #6)
    Last edited by Doogle; May 11th, 2011 at 02:44 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width