Relevance / Retrieving version of matching file

Appreciate if I can get help on this little problem.

I have a folder named C:\app\folder, this folder can contain a file that have a file name in the format AAxxx.exe. In case such a file exists, I want to fetch the version and the full path of the file.

This relevance works if a file exists, but if there is no file - I get the common “Error: Singular expression refers to nonexistent object.”

if (name of it of (file whose(name of it starts with "AA" AND name of it ends with ".exe") of folder "C:\app\folder") as string) != "" 
then ((version of it, pathname of it) of file whose(name of it starts with "AA" AND name of it ends with ".exe") of folder "C:\app\folder") As String
else "N/A"

Any idea how this can be solved, the idea is that the response should be “N/A” when no matching file exists.

Thanks,
Stefan

By far, it would be easier to take advantage of Plurals and leave an empty result if there are no matching files. That could be as simple as

(version of it, pathname of it) of files whose (name of it starts with "AA" AND name of it ends with ".exe") of folder "C:\app\folder")

If you absolutely must return the “N/A” string, there are a couple of details to be aware of -

  • When using an If/Then statement, the “Then” and “Else” clauses must return the same type of results - i.e. both strings, both a single tuple item
  • To overcome the “singular expression” issue, you must check for existence before returning the results.

I expect this should handle it

if (exists files whose(name of it starts with "AA" AND name of it ends with ".exe") of folder "C:\app\folder") then (((version of it, pathname of it) of files whose(name of it starts with "AA" AND name of it ends with ".exe") of folder "C:\app\folder") As String) else "N/A"
1 Like

Thanks for your quick reply Jason.

Both methods work fine when there it least one matching file, but in case there is not - I still get the “Error: Singular expression refers to nonexistent object.”

I am trying to write relevance that not return errors, the values are supposed to be presented in reports. Are there any other alternatives?

Thanks,
Stefan

You probably don’t have the folder in those error cases

(version of it, pathname of it) of files whose (name of it starts with "AA" AND name of it ends with ".exe") of folders "C:\app\folder")

or

if (exists folder "C:\app\folder" whose (exists files whose (name of it starts with "AA" AND name of it ends with ".exe") of it)) then (((version of it, pathname of it) of files whose (name of it starts with "AA" AND name of it ends with ".exe") of folder "C:\app\folder") As String) else "N/A"

1 Like

You are absolutely right, I had to check for the folder existence as well before checking for the files. This forum is amazing, thanks for all help!

1 Like

This forum is an incredible resource - I learned so much from reading posts written by some of the true greats of the Bigfix world - and I’m not going to attempt to name them because I’ll inevitably miss at least one.

1 Like