CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2001
    Location
    oulu, Finland
    Posts
    2

    how can i monitor a folder

    Hei,
    I want to monitor a Folder, that means if there is any new file is
    stored here, i can automatically know precisely these new files name, but i
    do not want know the by monitor detect the Directory disk size and then
    compare all the files names way, because i donot want to maintain a files
    list always. Does anybody knows is there any event can be detected when the
    folder is added a new file?
    My question is:
    There is a directory named ImageFile,
    and i want to write a program to monitor it always,
    and if a file Image1 is added into ImageFile folder by anybody, i can know
    it precisely and immediately, but i do not want to scan all the files in
    this folder, because maybe there are a huge number of files.
    Can i do it, and how?

    Many thanks.

    Michael.

    Email: [email protected]



    a free codes guru

  2. #2
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: how can i monitor a folder

    You might play with this a bit

    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: [email protected]
    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





    John G

  3. #3
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: how can i monitor a folder

    Hi John,
    I love this code. Get the rating from me.

    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: how can i monitor a folder

    Great code. Tomorrow I will rate you!

    Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  5. #5
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: how can i monitor a folder

    Now I can rate you!

    Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

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

Featured