Plugin time.spf

File: time.spf
Author: Nicholas Nemtsev
Description: This plugin provides the capability to monitor time of creation of (last access to, or last modification of) a file with to-a-second accuracy. It also provides additional capability to monitor changes in files matching a certain mask and to create a list of modified files.

New words:


FILE-TIME: "filename"
FILE-ATIME: "filename"
FILE-WTIME: "filename"

These words are used to get, respectively, creation time/last access time/last modification time of the specified file. The values are returned in FILETIME format.

Examples:

#( test_file_time1
\ if file "1.sem" was created earlier than "2.sem",
\ perform some useful operations
Action:
FILE-TIME: "C:\1.sem" FILE-TIME: "C:\2.sem" FTIME- 0>
IF
    \ ... performing some useful actions
THEN
)#

There also exist a postfix versions of these words:

S" c:\temp\test.txt" FILE-TIME
S" c:\temp\test.txt" FILE-ATIME
S" c:\temp\test.txt" FILE-WTIME

Additional methods of working with date and of conversion of dates between various formats are described in detail in Working with Dates and Time.


FTIME-

This word is used to compare the time when a file was created (or last accessed or modified) with that of another file or with the current time. It returns the difference (in seconds) between two dates in FILETIME format.

Examples:

\ file test.txt was created earlier than 10 minutes ago
FT-CUR FILE-TIME: "test.txt" FTIME- 600 >

Additional methods of working with date and of conversion of dates between various formats are described in detail in Working with Dates and Time.


FILE-CHANGE: "file_mask"

When used for the first time, word FILE-CHANGE: creates a list of files matching a specified mask, taking note of the last modification time; when this word is called later, it compares the data in the list with the current state of files and returns TRUE if one or more files has been changed. If none of the files has been changed, word FILE-CHANGE: returns FALSE.
The list of monitored files is saved in nnCron's home directory and is named etc\filechange.txt. Deleted files are not taken into account, in other words, they are not considered to be changed.
Word FILE-CHANGE: can be used with modifier RECURSIVE, which is placed before the word.

Please note that word FILE-CHANGE: returns a flag. That means that this word should be used with an IF ELSE THEN construct, or the value returned by FILE-CHANGE: should be explicitely dropped from the stack.

Examples:

#( test_file_time3
\ Every 10 minutes, checking for changes in all text files 
\ in directory 'c:\xxx'. Displaying a message if any changes found.
Time: */10
Action:
FILE-CHANGE: "c:\XXX\*.txt"
IF
    TMSG: "Files were changed!" 5
THEN
)#

#( test_file_time_3_recursive
\ Every 10 minutes, checking for changes in all text files 
\ in directory 'c:\xxx' and all of its subdirectories
\ Displaying a message if any changes found.
Time: */10
Action:
RECURSIVE
FILE-CHANGE: "c:\XXX\*.txt"
IF
    TMSG: "Files were changed!" 5
THEN
)#

FCH-CH-LIST

A list of modified files which is built during the execution of FILE-CHANGE:.

Examples:

#( test_file_time4
\ Checking for changes in all text files every 10 minutes
\ in directory 'c:\xxx'. Displaying a message if any changes found.
\ Printing the list of modified files to console
: type-node NodeValue ASCIIZ> TYPE CR ;
Time: */10
Action:
FILE-CHANGE: "c:\XXX\*.txt"
IF
    TMSG: "Files were changed" 5
    ['] type-node FCH-CH-LIST DoList
THEN
)#

#( test_file_time5
\ Every 10 minutes checking for changes in all text files 
\ in directory 'c:\xxx'. Saving the list of modified files to 
\ 'log\list.log' and displaying the list in a message box
: log-list NodeValue ASCIIZ> S" log\list.log" LOG ;
Time: */10
Action:
FILE-CHANGE: "c:\XXX\*.txt"
IF
    ['] log-list FCH-CH-LIST DoList
    TMSG: "Changed files:%crlf%%FILE: log\list.log%" 10
    FILE-DELETE: "log\list.log"
THEN
)#

#( test_file_time6
\ Every 10 minutes, checking for changes in all text files 
\ in directory 'c:\xxx'. Passing names of all modified files  
\ to word "START-APP:" as parameters for starting notepad.exe
: write-files NodeValue ASCIIZ> S" c:\changed_files.log" FAPPEND
  crlf S" c:\changed_files.log" FAPPEND
;
VARIABLE list-file
CREATE list-contents 258 ALLOT
Time: */10
Action:
FILE-CHANGE: "c:\XXX\*.txt"
IF
    \ writing the list of all the changed files
    ['] write-files FCH-CH-LIST DoList
    \ reading the list one line at a time
    S" c:\changed_files.log" R/O OPEN-FILE-SHARED THROW list-file !
    BEGIN list-contents 1+ 255 list-file @ READ-LINE THROW WHILE
        list-contents C!
        \ processing each line: passing each filename as 
        \ command line parameter
        START-APP: notepad.exe %list-contents COUNT%
    REPEAT
    DROP
    list-file @ CLOSE-FILE DROP
    FILE-DELETE: "c:\changed_files.log"
THEN
)#

UPTIME ( -- sec)

Puts on stack the number of seconds elapsed since startup of operating system.

Example:

\ the task will be started 
\ if the OS was started less then 90 seconds ago 
Rule: UPTIME 90 <