OH3: How to find out if file is younger than 60s

Hello,
I have an old (OH 2.5) rule where I parse a file within al folder.
In OH3 the “executeCommandLine” are not working any more, allthough I tried to adapt them to OH3.
They now look:

var String result
result = executeCommandLine(Duration.ofSeconds(1),"stat -t -c%Z /var/tmp/tdrm.log")

I need to check if a the file /var/tmp/tdrm.log is younger than e.g. 60s.
How can I do that?

Thanks,
Ingo

Use a small shell script like:

#!/bin/sh

NOW=`/bin/date "+%s"`
TIMESTAMPFILE=`/usr/bin/stat -t -c%Z /var/tmp/tdrm.log`
result=$(expr ${NOW} - ${TIMESTAMPFILE})
echo $result

to calculate the time(differnce).
Instead of echo $result you could add a check like

if [ $result -lt 60 ]
then
       echo "is younger than 60 seconds"
else
       echo "is not younger than 60 seconds"
fi

Thank you very much!!!
I will test it today!!!

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.