?
Solved

How to scan a directory automatically.

Posted on 2003-03-17
7
Medium Priority
?
532 Views
Last Modified: 2008-03-10
I like to know how to scan a directory automatically meaning if lets say directory A
got a file added.. it will maybe pop up a box to tell me that a file is created.
0
Comment
Question by:Lun
[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
7 Comments
 

Expert Comment

by:tejas_pathak
ID: 8151146
I guess, u will need to create a service for that.
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 600 total points
ID: 8151172
Hi Lun,

Here is a basic example, you will need to extend this somewhat to get precisely what you want.

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_SIZE = &H8
Private Const FILE_NOTIFY_CHANGE_LAST_WRITE = &H10
Private Const FILE_NOTIFY_CHANGE_SECURITY = &H100
Private Const FILE_NOTIFY_CHANGE_ALL = &H4 Or &H2 Or &H1 Or &H8 Or &H10 Or &H100
Private Declare Function FindFirstChangeNotification Lib "kernel32" Alias "FindFirstChangeNotificationA" (ByVal lpPathName As String, ByVal bWatchSubtree As Long, ByVal dwNotifyFilter As Long) As Long
Private Declare Function FindCloseChangeNotification Lib "kernel32" (ByVal hChangeHandle As Long) As Long
Private Declare Function FindNextChangeNotification 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 ResetEvent Lib "kernel32" (ByVal hEvent As Long) As Long
Private Sub Form_Load()
    'KPD-Team 2000
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    Dim Ret As Long
    'Set the notification hook
    Ret = FindFirstChangeNotification("C:\", &HFFFFFFFF, FILE_NOTIFY_CHANGE_ALL)
    'Wait until the event is triggered
    WaitForSingleObject Ret, &HFFFFFFFF
    MsgBox "Event Triggered for the first time"
    'Reactivate our hook
    FindNextChangeNotification Ret
    'Wait until the event is triggered
    WaitForSingleObject Ret, &HFFFFFFFF
    MsgBox "Event Triggered for the second time"
    'Remove our hook
    FindCloseChangeNotification Ret
End Sub


Tim Cottee MCSD, MCDBA, CPIM
http://www.timcottee.tk 

Brainbench MVP for Visual Basic
http://www.brainbench.com

Experts-Exchange Advisory Board Member
0
 
LVL 1

Assisted Solution

by:Fischermaen
Fischermaen earned 600 total points
ID: 8151213
Hello Lun,

you don't need a service for that. You have to register to the shell.

Put following code to your Form_Load-Event:

'Code BEGIN
Call SHNotify_Register(g_objMyAddMan.hWnd)
'Code END

And following code to your Form_Unload-Event:

'Code BEGIN
Call SHNotify_Unregister
'Code END

Add a module to your project and add following code to it:

'Code BEGIN
'the one and only shell change notification
'handle for the desktop folder
Private m_hSHNotify As Long

'the desktop's pidl
Private m_pidlDesktop As Long

'User defined notification message sent
'to the specified window's window proc.
Public Const WM_SHNOTIFY = &H401

'------------------------------------------------------
Public Enum SHCN_EventIDs
   SHCNE_RENAMEITEM = &H1          '(D) A non-folder item has been renamed.
   SHCNE_CREATE = &H2              '(D) A non-folder item has been created.
   SHCNE_DELETE = &H4              '(D) A non-folder item has been deleted.
   SHCNE_MKDIR = &H8               '(D) A folder item has been created.
   SHCNE_RMDIR = &H10              '(D) A folder item has been removed.
   SHCNE_MEDIAINSERTED = &H20      '(G) Storage media has been inserted into a drive.
   SHCNE_MEDIAREMOVED = &H40       '(G) Storage media has been removed from a drive.
   SHCNE_DRIVEREMOVED = &H80       '(G) A drive has been removed.
   SHCNE_DRIVEADD = &H100          '(G) A drive has been added.
   SHCNE_NETSHARE = &H200          'A folder on the local computer is being
                                   '    shared via the network.
   SHCNE_NETUNSHARE = &H400        'A folder on the local computer is no longer
                                   '    being shared via the network.
   SHCNE_ATTRIBUTES = &H800        '(D) The attributes of an item or folder have changed.
   SHCNE_UPDATEDIR = &H1000        '(D) The contents of an existing folder have changed,
                                   '    but the folder still exists and has not been renamed.
   SHCNE_UPDATEITEM = &H2000       '(D) An existing non-folder item has changed, but the
                                   '    item still exists and has not been renamed.
   SHCNE_SERVERDISCONNECT = &H4000 'The computer has disconnected from a server.
   SHCNE_UPDATEIMAGE = &H8000&     '(G) An image in the system image list has changed.
   SHCNE_DRIVEADDGUI = &H10000     '(G) A drive has been added and the shell should
                                   '    create a new window for the drive.
   SHCNE_RENAMEFOLDER = &H20000    '(D) The name of a folder has changed.
   SHCNE_FREESPACE = &H40000       '(G) The amount of free space on a drive has changed.

#If (WIN32_IE >= &H400) Then
   SHCNE_EXTENDED_EVENT = &H4000000 '(G) Not currently used.
#End If

  SHCNE_ASSOCCHANGED = &H8000000   '(G) A file type association has changed.
  SHCNE_DISKEVENTS = &H2381F       '(D) Specifies a combination of all of the disk
                                   '    event identifiers.
  SHCNE_GLOBALEVENTS = &HC0581E0   '(G) Specifies a combination of all of the global
                                   '    event identifiers.
  SHCNE_ALLEVENTS = &H7FFFFFFF
  SHCNE_INTERRUPT = &H80000000     'The specified event occurred as a result of a system
                                   'interrupt. It is stripped out before the clients
                                   'of SHCNNotify_ see it.
End Enum

Public Type PIDLSTRUCT
  'Fully qualified pidl (relative to
  'the desktop folder) of the folder
  'to monitor changes in. 0 can also
  'be specified for the desktop folder.
   PIDL As Long
   
  'Value specifying whether changes in
  'the folder's subfolders trigger a
  'change notification event (it's actually
  'a Boolean, but we'll go Long because
  'of VB's DWORD struct alignment).
   bWatchSubFolders As Long
End Type

Public Declare Function SHChangeNotifyRegister Lib "shell32" Alias "#2" _
   (ByVal hWnd As Long, _
    ByVal uFlags As SHCN_ItemFlags, _
    ByVal dwEventID As SHCN_EventIDs, _
    ByVal uMsg As Long, _
    ByVal cItems As Long, _
    lpps As PIDLSTRUCT) As Long


Public Function SHNotify_Register(hWnd As Long) As Boolean
 
  'Registers the one and only shell change notification.

   Dim ps As PIDLSTRUCT
 
  'If we don't already have a notification going...
   If (m_hSHNotify = 0) Then
 
     'Get the pidl for the desktop folder.
      m_pidlDesktop = GetPIDLFromFolderID(0, CSIDL_FAVORITES)
     
      If m_pidlDesktop Then
     
        'Fill the one and only PIDLSTRUCT, we're
        'watching desktop and all of the it's
        'subfolders, everything...
         ps.PIDL = m_pidlDesktop
         ps.bWatchSubFolders = True
         
        'Register the notification, specifying that
        'we want the dwItem1 and dwItem2 members of
        'the SHNOTIFYSTRUCT to be pidls. We're
        'watching all events.
         m_hSHNotify = SHChangeNotifyRegister(hWnd, _
                                              SHCNF_TYPE Or SHCNF_IDLIST, _
                                              SHCNE_CREATE Or SHCNE_DELETE Or SHCNE_MKDIR Or SHCNE_RENAMEFOLDER Or SHCNE_RENAMEITEM Or SHCNE_RMDIR Or SHCNE_UPDATEDIR Or SHCNE_UPDATEITEM Or SHCNE_INTERRUPT, _
                                              WM_SHNOTIFY, _
                                              1, _
                                              ps)
                                             
         SHNotify_Register = CBool(m_hSHNotify)
   
    Else
   
        'If something went wrong...
         Call CoTaskMemFree(m_pidlDesktop)
   
    End If
   
  End If
 
End Function

Public Function SHNotify_Unregister() As Boolean
 
  'Unregisters the one and only shell change notification.
 
  'If we have a registered notification handle.
   If m_hSHNotify Then
   
     'Unregister it. If the call is successful,
     'zero the handle's variable, free and zero
     'the the desktop's pidl.
      If SHChangeNotifyDeregister(m_hSHNotify) Then
     
         m_hSHNotify = 0
         Call CoTaskMemFree(m_pidlDesktop)
         m_pidlDesktop = 0
         SHNotify_Unregister = True
         
      End If
     
   End If

End Function

Public Function GetPathFromPIDL(PIDL As Long) As String

  'Returns a path from only an absolute pidl
  '(relative to the desktop).

   Dim spath As String * MAX_PATH
   
  'SHGetPathFromIDList rtns TRUE (1),
  'if successful, FALSE (0) if not
   If SHGetPathFromIDList(PIDL, spath) Then
      GetPathFromPIDL = GetStrFromBufferA(spath)
   End If
   
End Function


Public Function GetStrFromBufferA(sz As String) As String

   'Return the string before first null
   'char encountered (if any) from an
   'ANSII string. If no null, return the
   'string passed
 
   If InStr(sz, vbNullChar) Then
         GetStrFromBufferA = Left$(sz, InStr(sz, vbNullChar) - 1)
   Else: GetStrFromBufferA = sz
   End If
   
End Function

Public Function WindowProc(ByVal hWnd As Long, _
                           ByVal message As Long, _
                           ByVal wParam As Long, _
                           ByVal lParam As Long) As Long
     
   Select Case Parameter(message)
       Case WM_SHNOTIFY
            Dim strPath       As String
            Dim shns          As SHNOTIFYSTRUCT
                                       
           'Fill the SHNOTIFYSTRUCT from it's pointer.
            Call CopyMemory(shns, _
                            ByVal wparam, _
                            Len(shns))
                                                                             
            Select Case Parameter(lParam)
              Case SHCNE_DELETE, _
                   SHCNE_RMDIR
                  strPath = UCase$(GetPathFromPIDL(shns.dwItem1))
                   Case SHCNE_CREATE, _
                        SHCNE_MKDIR, _
                        SHCNE_UPDATEITEM
              End Select
        End Select
End Function
'Code END
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Lun
ID: 8171037
I dont really understand. Okay erm, I want to make a program that can autosend a file in a directory to another directory every 1 minute. Which means I need to have the autoscan directory program code too.

Can someone help me please?
0
 
LVL 1

Expert Comment

by:Fischermaen
ID: 8172237
Hi Lun,

the code snippet I provided, give you the ability to monitor a directory (and it's subdirectories, if you want to). That means, everytime the user or another process (even your own one) changes a file, creates a file or directory, deletes a file or directory, and so on, you will be notified. Be careful (and fast) in the method call, because this is the message loop of your window, and the explorer will wait until you quit the notification of a directoy change.

If you have any more question, just ask.

Andy
0
 

Expert Comment

by:CleanupPing
ID: 8900010
Lun:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
0
 
LVL 6

Expert Comment

by:GPrentice00
ID: 8956529
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

-->Split between Fischermaen and TimCottee

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

GPrentice00
EE Cleanup Volunteer
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

777 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