English Amiga Board

English Amiga Board (https://eab.abime.net/index.php)
-   request.Other (https://eab.abime.net/forumdisplay.php?f=75)
-   -   Watch files for changes (https://eab.abime.net/showthread.php?t=106771)

KONEY 29 April 2021 19:50

Watch files for changes
 
Can anyone suggest an utility and/or and ARexx script to monitor a file for changes and eventually trig some events? Aminet didn't help me...

thanks!

daxb 29 April 2021 20:33

What do you mean by monitor and what kind of changes exactly? With ARexx you can use STATEF() to compare file size, protection flags, time stamp and comment. Or you use checksum to compare. Should it work only while the program or script is running or does it need a database to compare with?

KONEY 29 April 2021 22:00

When a file is changed by any other program, so I guess timestamp is good.

I'll give a try to the AX command, thanks!

thomas 29 April 2021 22:08

dos.library has a function to get notified when a file is changed. IPrefs uses it for example to update prefs when a prefs editor exits with Use or Save.

But I am not aware of a utility which allows general use of this function. It shouldn't be too difficult to write one, though.

daxb 30 April 2021 11:29

It seems RMH (RexxMustHave http://aminet.net/package/util/rexx/rmh ) library contains notify functions that can be used for files. Yes, there is also an example notify.rexx.

KONEY 30 April 2021 19:45

daxb suggestion turned out to be of great value, with the power of ARexx do what I needed was as simple as this:

Code:

/* watch a file and execute it, useful to test code compiled externally */

SAY "WATCH A FILE FOR CHANGES AND EXECUTE IN CASE"
SAY "ARexx script by KONEY 2021"
SAY "--------------------------------------------------"

library = 'rexxsupport.library'
IF ~SHOW( 'L', library ) THEN DO
        IF ~ADDLIB( library, 0, -30, 0 ) THEN DO
                SAY 'Failed to add library ' || library || '.'
                EXIT 10
        END
END

initial_file =  STATEF("j")
SAY initial_file
loop=1
SAY "WATCHING..."
DO until loop > 1
        actual_file =  STATEF("j")
        IF actual_file ~=  initial_file THEN        DO
                SAY "CHANGES DETECTED!"
                ADDRESS COMMAND  "J  >NIL:"
                initial_file =  STATEF("j")
                END
END

EXIT

:great:great

daxb 30 April 2021 20:36

The problem is the "stupid" endless loop that has to be used. I would at least DELAY() the loop to save some CPU time. I've never tried but the RMH notify seems to be a better way:
Code:

/*
    Name:          notify.rexx
    Template:      FILE,CLIP=CLIPUNIT/K/N,TIMEOUT/K/N
    Description:    wait for a changes on a FILE or a CLIP with timeout
                    default for CLIP is 0
                    default for TIMEOUT is 10 seconds
*/

parm.1.value=0
parm.2.value=10

if ~RMHReadArgs("FILE,CLIP=CLIPUNIT/K/N,TIMEOUT/K/N") then do
    call PrintFault(IoErr(),ProgramName("NOEXT"))
    exit
end

if parm.1.value<0 | parm.2.value<0 | parm.2.value>100 then do
    call PrintFault(115,ProgramName("NOEXT"))
    exit
end

if ~parm.0.flag | parm.1.flag then name="CLIP"
else name=parm.0.value

if name=="CLIP" then noti=StartNotify(name,parm.1.value)
else noti=StartNotify(name)
if noti<0 then do
    call PrintFault(IoErr(),ProgramName("NOEXT"))
    exit
end
ns=NotifySignal(noti)

timer=CreateTimer()
ts=TimerSignal(timer)

call StartTimer(timer,parm.2.value)
mask=wait(or(ns,ts))
if and(mask,ns)~=0 then do
    if name=="CLIP" then do
        say "Clip unit" parm.1.value "changed"
        if ReadTextClip("C")~=0 then do
            say "Clip unit" parm.1.value "content is:"
            say c
        end
    end
    else say "File:" name "changed"
end
else if and(mask,ts)~=0 then say "Timeout"
call FreeNotify(noti)


KONEY 30 April 2021 20:57

Thanks, I agree. But when I'm executing this I'm basically waiting for some code to be compiled into the "J" executable which when is ran it takes over the OS, until exited then waits for some code... in loop :)


All times are GMT +2. The time now is 12:28.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.

Page generated in 0.04818 seconds with 11 queries