I have a text file called file.txt
that is being modified every few seconds by an external program (my Stenography machine's software).
myCode
is a program which takes as input a List
called data
which is Import
ed from file.txt
.
How can I execute myCode
everytime this file is modified?
The following code does not work but looks like what I need.
file = NotebookDirectory[] <> "file.txt";
Dynamic[
Refresh[
fileSize := FileByteCount[file];
data = Import[file, "Table"];
myCode[data]
,TrackedSymbols :> {fileSize}
]
]
to help de-abstract the problem, here is are two example cases of
myCode
andfile.txt
myCode[data_] := Style[ListLinePlot[Flatten[data], PlotTheme -> "Marketing"], Magnification -> 2]; Export[NotebookDirectory[]<>"file.txt", {1, 2, 3}]
This question has been asked 8 years ago for Mathematica 9.0. Sadly the answer provided then does not work in Mathematica 12.0
Note: I am not interested in repeatedly executing myCode
every $n$ seconds. I only want to execute myCode
IF file.txt
is modified.
I'm afraid I oversimplified my question above. I wanted to try and present a minimal example. My true problem is below:
myCode
is a program which takes the last element of data
(a list of integers Import
ed from file.txt
) and computes the ratio with the last element of another list (imported from another file which is sometimes modified (when this second file is modified it is modified at the same time $t$ as when file.txt
is modified)). myCode
then AppendsTo
this ratio to a third list (which is then plotted). I only want it to do this operation if file.txt
is modified.