Read the text/Log files continues

Hi ,

I'm trying to read the log files of MES transaction logs in the C drive program file location, In that location multiple log files will save based on the MES transaction.

In that, I need to read the most recent log files and second think that file has a Text and XML data will be there. so it is possible to read the specified text in that document what transaction happens on the MES side I need to read that text format data. then it is possible to store that data in a database or write that data to any memory tag.

Based on the data itself I need to trigger the PLC tags.

so, what is the best approach could you please help with this guys?

Use a gateway timer event script. Use standard java APIs to scan the folder for files. Examine the names and timestamps against the most recent files placed in the database, to distinguish new from old. Process new files with jython as desired.

Hi pturmel,

Thanks for your reply,

I didn't get your approach could you please explain with examples or in a detailed way?

I'm trying to test the script in the script console the script below is running fine the same script if I'm given any one of the tag values changed script the script is not working.

import os

Define the directory containing the log files

directory = "C:\Test\"

Specify the text you want to search for

specified_text = "text"
try:
# Get a list of all files in the directory
files = os.listdir(directory)

# Filter out non-log files
log_files = [file for file in files if file.endswith(".log")]

if log_files:
    # Get the most recent log file based on modification time
    most_recent_log = max(log_files, key=lambda x: os.path.getmtime(os.path.join(directory, x)))
    
    # Read the content of the most recent log file
    most_recent_log_path = os.path.join(directory, most_recent_log)
    with open(most_recent_log_path, 'r') as file:
        file_content = file.read()
    
    # Check if the specified text is present in the content of the most recent log file
    if specified_text in file_content:
    	tagpaths = "[default]S71200_PLC/Start (HMI)"
    	system.tag.writeBlocking(tagpaths, True)
    	print("Specified text found in the most recent log file.:",most_recent_log)
    else:
        print("Specified text not found in the most recent log file.")

Please edit your comment (pencil icon), highlight all of the code, and press the "preformatted text" button. That will enclose your code in markers that preserve formatting for us bystanders.

That said, I don't see anything in your code that checks file names against a database of already-processed files.

You have a text search operation within files you are finding, but that is something that should only be done on files that haven't been processed yet.

Your code appears to be written at the "top" level. Which suggests you are trying to write this code directly in an event. Don't do that. Use a library script, and place your code in a function, and make a one-line call to the function from the timer event.