Opening a file

Hello, my name is Stuart Drollinger

I am trying to have a table, open a file automatically. That is not in the database. I have a table inside of ignition that is a bunch of product numbers or machine numbers. This gives me the first 9-10 characters of the file. But the problem is, in the folder I want to open from, it is on a public drive so people have access to it. Unfortunately the names inside of the folder are slightly different, here is an example:

Ignition shows it as this. 20-000024 the folder has it like this. 20-000024-REV A-12oz Hot Soup Cup.pdf

The revision Letter can change, so I need a way to select the most likely one, or something similar to that. There will ever only be one version of 20-000024 in this folder, because there is an archives folder and as the engineers make new revisions they replace the current ones. Is there a function or expression or do you know of a way to do what I am aiming to do?

Here’s an example of how to get a list of files in a directory, and then extract the one(s) that start with a certain string:

import os path="C:\\Users\\Administrator\\Desktop" dirList=os.listdir(path) for fname in dirList: if fname.startswith("20-000024"): break

You would then have “fname” variable with the filename that you were looking for stored as a string. This assumes, of course, that there is only one file starting with that string, which you did mention is the case. Also, the file path will have to be accessible by all clients, unless you use a gateway script.

Concatenate “fname” with “path”, and then you can open the file however you want. Are you trying to open the file within Ignition, or just open the file in its native default program? If you using a PDF viewer of some sort within Ignition, just set the file path property to the newly created file path.

Only trying to open the file in the default setting, so a pdf which they all are. would get opened in adobe reader most likely. And we also want to only allow the people who have access to the folder to be able to open it.

Thank you very much.

Take a look at in the manual, that will open the pdf in the default program. For permissions checking, the best way will probably be to wrap the os.listdir() operation in a try/except:

try: dirList=os.listdir(path) except: # print error message box, warning box, something...

That should capture the instance when a user doesn’t have permissions to open a folder. If not, you could also try setting up a custom role in your project’s authentication profile, assign the role only to users that should have access to the folder, and then wrap the os.listdir() call with a conditional statement checking to see if the current logged in user has the proper role.

if event.clickCount>=2 and event.source.selectedRow!=-1:
import os
docNumber = event.source.selectedDocumentSQL
path = “P:\Engineering\MGP Drawings\PDF\”
for fname in dirList:
if fname.startswith(docNumber):
filePath = path+fname

That is what it looks like. and it works out very nicely. If the user doesnt have permissions it already shows a error box saying they dont. and if there isnt a file it doesnt even try to open it.

Thank you so much for your time.