Using Win32 functions in Visual FoxPro Image Gallery
File Management
..msdn
CopyFile
CopyFileTransacted
CreateDirectory
CreateFile
CreateFileTransacted
CreateHardLink
CreateSymbolicLink
DeleteFile
DeleteFileTransacted
FindClose
FindCloseChangeNotification
FindFirstChangeNotification
FindFirstFile
FindNextChangeNotification
FindNextFile
FlushFileBuffers
GetBinaryType
GetCurrentDirectory
GetDriveType
GetFileAttributes
GetFileAttributesEx
GetFileInformationByHandle
GetFileType
GetFullPathName
GetLongPathName
GetShortPathName
GetTempFileName
GetTempPath
LockFile
MoveFile
MoveFileTransacted
OpenFile
QueryDosDevice
ReadDirectoryChangesW
ReadFile
RemoveDirectory
RemoveDirectoryTransacted
ReplaceFile
SearchPath
SetCurrentDirectory
SetEndOfFile
SetFileAttributes
SetFilePointer
SetFilePointerEx
SetSearchPathMode
UnlockFile
WriteFile
_lclose
_lopen
Code examples:
Monitoring changes in a directory
Monitoring changes in a directory

User rating: 9/10 (1 votes)
Rate this code sample:
  • ~
More code examples    Listed functions    Add comment     W32 Constants      Translate this page Printer friendly version of this code sample
Before you begin:
Imagine that an application needs to be notified upon file changes in a particular directory: files added or deleted, changed, or accessed. The ChangeNotification function is an answer.

After starting this code sample, try to add, change or delete files in the target directory (must be valid path). Create a FoxPro table in it and operate with its records.

See also::
  • FileSystemWatcher ActiveX Control for Visual FoxPro
  • Monitoring changes occurring within a directory
  • Enumerating files opened on the network
  •  
    PUBLIC oForm
    oForm = CREATEOBJECT("Tform", SYS(2023)) && watching TEMP dir
    oForm.Visible = .T.
    * end of main
     
    DEFINE CLASS Tform As Form
    #DEFINE FILE_NOTIFY_CHANGE_FILE_NAME 1
    #DEFINE FILE_NOTIFY_CHANGE_DIR_NAME 2
    #DEFINE FILE_NOTIFY_CHANGE_ATTRIBUTES 4
    #DEFINE FILE_NOTIFY_CHANGE_SIZE 8
    #DEFINE FILE_NOTIFY_CHANGE_LAST_WRITE 16
    #DEFINE FILE_NOTIFY_CHANGE_LAST_ACCESS 32
    #DEFINE FILE_NOTIFY_CHANGE_CREATION 64
    #DEFINE FILE_NOTIFY_CHANGE_SECURITY 128
    #DEFINE INVALID_HANDLE_VALUE -1
    #DEFINE WAIT_OBJECT_0 0
    #DEFINE WATCHING_INTERVAL 1000  && milliseconds
     
    PROTECTED hNotify, PathBeingWatched
     
        Width=400
        Height=150
        MaxButton=.F.
        BorderStyle=2
        AutoCenter=.T.
        Caption="Watching directory"
     
        hNotify=INVALID_HANDLE_VALUE
        PathBeingWatched=""
     
        ADD OBJECT lblTarget As Label WITH Left=10, Top=7, Autosize=.T.
        ADD OBJECT tm As Timer WITH Interval=0
     
        ADD OBJECT lblAlert As Label WITH Left=10, Top=30,;
        Autosize=.T., Caption="Notification:"
     
    FUNCTION Init(cPath)
        THIS.declare
        THIS.PathBeingWatched = FULLPATH(m.cPath)
     
        IF Not THIS.StartWatching()
            = MESSAGEBOX("Notification handle error.")
            RETURN .F.
        ENDIF
     
    PROCEDURE Destroy
        THIS.StopWatching
     
    PROTECTED FUNCTION StartWatching
        LOCAL lResult
     
        * no subdirs watched
        THIS.hNotify = FindFirstChangeNotification(;
            THIS.PathBeingWatched, 0,;
            FILE_NOTIFY_CHANGE_FILE_NAME +;
            FILE_NOTIFY_CHANGE_LAST_WRITE )
     
        lResult = (THIS.hNotify <> INVALID_HANDLE_VALUE)
     
        IF lResult
            THIS.lblTarget.Caption = "Watched: " +;
                THIS.PathBeingWatched
            THIS.tm.Interval = WATCHING_INTERVAL
        ENDIF
    RETURN  lResult
     
    PROTECTED PROCEDURE ContinueWatching
        IF FindNextChangeNotification(THIS.hNotify) = 0
            THIS.StopWatching
            = MESSAGEBOX("Request error.")
            THIS.Release
        ENDIF
        THIS.tm.Interval = WATCHING_INTERVAL
     
    PROTECTED FUNCTION StopWatching
        THIS.tm.Interval = 0
        IF THIS.hNotify <> INVALID_HANDLE_VALUE
            = FindCloseChangeNotification(THIS.hNotify)
        ENDIF
     
    FUNCTION _signaled        && returns signaled state
    RETURN (WaitForSingleObject(THIS.hNotify, 0) = WAIT_OBJECT_0)
     
    PROCEDURE _notify        && triggered on event
        LOCAL cMessage
        cMessage = "Notification: " + TTOC(DATETIME())
        THIS.lblAlert.Caption = cMessage
        ACTIVATE SCREEN
        ? cMessage
        THIS.ContinueWatching
     
    PROCEDURE tm.Timer
        IF ThisForm._signaled()
            ThisForm._notify
        ENDIF
     
    PROTECTED PROCEDURE declare
        DECLARE SHORT FindNextChangeNotification IN kernel32;
            INTEGER hChangeHandle
     
        DECLARE SHORT FindCloseChangeNotification IN kernel32;
            INTEGER hChangeHandle
     
        DECLARE INTEGER FindFirstChangeNotification IN kernel32;
            STRING lpPathName, INTEGER bWatchSubtree,;
            INTEGER dwNotifyFilter
     
        DECLARE INTEGER WaitForSingleObject IN kernel32;
            INTEGER hHandle, INTEGER dwMilliseconds
     
    ENDDEFINE
     
     
     

    User rating: 9/10 (1 votes)
    Rate this code sample:
    • ~
    2819 bytes  
    Created: 2001-08-23 12:00:00  
    Modified: 2011-02-16 13:28:43  
    Visits in 7 days: 116  
    Listed functions:
    FindCloseChangeNotification
    FindFirstChangeNotification
    FindNextChangeNotification
    WaitForSingleObject
    Printer friendly API declarations
    Word Index links for this example:
    Translate this page:
      Spanish    Portuguese    German    French    Italian  
    FreeTranslation.com offers instant, free translations of text or web pages.
    User Contributed Notes:
    There are no notes on this subject.


    Copyright © 2001-2013 News2News, Inc. Before reproducing or distributing any data from this site please ask for an approval from its owner. Unless otherwise specified, this page is for your personal and non-commercial use. The information on this page is presented AS IS, meaning that you may use it at your own risk. Microsoft Visual FoxPro and Windows are trade marks of Microsoft Corp. All other trademarks are the property of their respective owners. 

    Privacy policy
    Credits: PHP (4.4.9), an HTML-embedded scripting language, MySQL (5.1.55-log), the Open Source standard SQL database, AceHTML Freeware Version 4, freeware HTML Editor of choice.   Hosted by Korax Online Inc.
    Last Topics Visited (72.44.48.122)
    3 sec.Example: 'Monitoring changes occurring within a directory'
    4.98 hrs.Function: 'mmioDescend'
    Function group: 'Windows Multimedia'
    5.6 hrs.Function: 'NetShareAdd'
     Example: 'Retrieving information about the main VFP window'
    9.49 hrs.Function: 'IsWow64Process'
    Function group: 'Process and Thread'
    10.75 hrs.Example: 'Accessing the list of Windows Recent Documents'
    11.09 hrs.Function: 'SHEnumKeyEx'
     Example: 'Enumerating printer drivers installed'
    12.61 hrs.Example: 'Switching between keyboard layouts'
     Function: 'JetRetrieveColumn'
    Function group: 'Extensible Storage Engine (ESE, Jet Blue)'
    Google
    Advertise here!