Log file directory listing and monitoring

Hi,

I’m looking to monitor a specific directory that contains log files. I want to check the number of files that are present, as well as their dates and sizes.

Is there a function for that? I was hoping to find something in system.file but found nothing that could do what I want.

Thanks!

There is no built in function for monitoring a directory. I would recommend a Gateway timer script that iterates through the desired directory and saves the results to the database so that the next iteration, it can compare and see what if any changes happened. I would probably use the os jython library.

import os
b = os.path.getsize("/path/isa_005.mp3")

b would give you the size in bytes. So you’ll probably want to write a function to convert from bytes to kilobytes or megabytes or however you want to view it.

You can also use os to iterate through a directory with os.walk and modify the logic

for (root,dirs,files) in os.walk('someDirectory', topdown=true):
    print root
    print dirs
    print files

You’ll have to craft the logic yourself but I think these two functions should get you pretty much there (along with any help functions you find yourself needing).

Last note - if you know when you expect file changes, ie when some button is pushed or some other part of your Ignition application is run, you might prefer a gateway message handler to check at that moment, and perhaps to keep a gateway timer script with a long interim that runs in case of any misses.

Edit: To get date modified of the files - use os.path.getmtime(filepath)

Thanks, this works great!

1 Like