Linking Power table Cell to an external file

I wanted to bounce ideas off you guys and I don't know if there is way to do this in ignition but I have a script that opens certain files from a folder if clicked on a cell, but the postfix of the folder is in format _XXXXXXX. My problem is I have no way of knowing the postfix, the closest way I can separate the folders is the "Date Modified" filter in Windows file explorer which corresponds to a DAte/Time column I have in power table.

In short there are two rows in power table with seria number a, but I want to open files from the first folder from cells in one row and files from the second folder from cells in the other row. And I have info about the date and time to when each folder was created in the power table.

Any help or suggestion is appreciated. Thanks.

	import os
	import system
	if colName not in ["Serial Number", "Result"]:  
	        return
	
	serial = self.data.getValueAt(rowIndex, self.data.getColumnIndex("Serial Number"))
	location = self.data.getValueAt(rowIndex, self.data.getColumnIndex("Test Stand"))
	folder_path = "A:\\Result\\M54\\Test\\AT\\" + location + "\\" + serial + "\\" + serial + "_XXXXXXXX"
	
	# Function to find the first file in the specified folder
	
	ext = ""
	if colName == "Serial Number":
		ext = ".html"
	elif colName == "Result":
		ext = ".txt"
	
	
	def find_file(folder_path, ext):
		for file_name in os.listdir(folder_path):
	 	    if file_name.endswith(ext):
		        return os.path.join(folder_path, file_name)
		return None
	# Find the HTML file
	file_path = find_file(folder_path, ext)
	
	
   	print("File path:", file_path)
   	
   	# Open the file using system.net.openURL
	if file_path:
	 	try:
	  	    system.net.openURL("file:///" + file_path.replace("\\", "/"))
	 	except Exception as e:
	  	    system.gui.errorBox("Failed to open file: {}".format(str(e)))
	else:
	 	system.gui.warningBox("No file found in the specified folder.")

I can't really understand what you're asking for or currently doing with this script.

Can you either explain more, or boil down the thing you're asking to do further?

What does the actual table in Ignition look like - maybe that will help?

The answer might be something something regular expressions but it's really hard to know that without better understanding of the problem.


So this is what the table looks like, the script above opens the report for the test in HTML format if they click on the corresponding serial number cell or in a TXT format if click on the corresponding result cell.

My problem is when a test is ran, it is creating the folders named as "1A912AA_202412111121" and "1A912AA_202412111131" and "1A912AA_202412081132" and so on i.e date and time as a postfix to the serial number. So I cannot direct the folder path to the correct folder i.e "_XXXXXXXX" is unknown. The date and time in the table and the postfix are close but not quite exact so I cannot use that.

So, I'm trying to learn how would I direct the path to open that folder because if I harcode the postfix on the folder path to the script like below and click on the 1A912AA cell, it is going to open that report.

folder_path = "A:\\Result\\M54\\Test\\AT\\" + location + "\\" + serial + "\\" + serial + "_202412111131".

I hope that explains it better.

Okay, wait, let me try and restate and see if I'm following.

You have a folder that's getting files written into it, presumably by something out of your direct control.

Based on your first post, this folder structure is something like (please correct me on any mistaken assumptions):

Result/M54/Test/AT/
	a_CASDAQWEA/ # location?
		1A912AA/ # serial number?
			1A912AA_202412111121/ # serial number + datetime?
				report.txt
				report.html
			1A912AA_202412081132/
				report.txt
				report.html
		IA611AC/
			.../
			.../
			.../

And so the problem at hand is, when someone double clicks on the first row in your screenshot, you want to open the html/text report for the most recent run (or whatever) of your process?

Again, I'm making a lot of assumptions because I don't quite know the process.
My biggest question is how the folders shown in your first screenshot (a_CASDAQWEA) correspond to the location column shown in your screenshot of the table (A100) -- unless that's exactly what you're trying to do and the question you're asking. If the only signal you have is the modification date(s), and they don't align precisely with the values you have in the power table, I don't see how it can be made to consistently open the correct folder.

It's Vision. A filesystem browse could be performed on demand.

Sure, in theory - if the requisite information actually exists to unambiguously locate the right file/folder given the information in the power table row. That's where I'm hung up, I think.

Yes your assumptions are all correct. The location would be same as on the table and not as the screenshot, (please ignore the files screenshot) i.e

Result/M54/Test/AT/
	A100/ # location
		1A912AA/ # serial number
			1A912AA_202412111121/ # serial number + datetime (failed test)
				report.txt
				report.html
			1A912AA_202412081132/# serial number (passed test)
				report.txt
				report.html
		IA611AC/
			.../
			.../
			.../

And as you can see on the table 1A912AA was ran twice and it failed once and passed once. So when they click on the serial number of passed test, it should open the report from 1A912AA_202412081132 folder and when they click on the failed serial number it should open the report from the
1A912AA_202412081121 folder.

Okay, so Ignition doesn't have a lot built in to help you here directly, but it doesn't sound impossible. You want to build up a path to your root test folder, then pair your known location + serial number data to it, then (the most important bit) look within that subfolder (e.g. the top level 1A912AA/ folder in the example), one level deep, and find the 'most recent' report.

That's a relatively simple operation - list all the folders within the directory, split their name on the underscore, convert the digit sequence to a number, and sort on it.

I haven't tested it and I can't personally vouch for it, but from a quick skim this code looks pretty much correct:

1 Like

So, I couldn't see the generated code for some reason, but following your description and using your prompt and editing the result it worked, appreciate it a lot @PGriffith.

 #Function to extract timestamp from directory name
	def extract_timestamp(dir_name):
	    match = re.search(r'\[(\d{8}_\d{6})\]', dir_name)
	    if match:
	        return int(re.sub(r'\D', '', match.group(1)))
	    return None
	
	# Function to find the folder with the nearest timestamp
	def get_nearest_folder(directory_path, target_timestamp):
	    # Convert the input string to a Path object
	    dir_path = Paths.get(directory_path)
	    
	    # List all entries in the given directory
	    entries = Files.list(dir_path).collect(Collectors.toList())
	    
	    # Filter only directories
	    directories = ArrayList()
	    for entry in entries:
	        if Files.isDirectory(entry):
	            directories.add(entry)
	    
	    # Find the directory with the nearest timestamp
	    nearest_dir = None
	    min_diff = float('inf')
	    for dir in directories:
	        timestamp = extract_timestamp(dir.getFileName().toString())
	        if timestamp:
	            diff = abs(target_timestamp - timestamp)
	            if diff < min_diff:
	                min_diff = diff
	                nearest_dir = dir.getFileName().toString()
	    
	    return nearest_dir
	
	# Find the nearest folder
	closest = get_nearest_folder(base_folder_path, target_timestamp)
	
	
	print("Closest Folder:", closest)
1 Like