NetXMS Support Forum

English Support => General Support => Topic started by: stoffmann on November 16, 2022, 01:29:34 PM

Title: Best way to check if a file exists
Post by: stoffmann on November 16, 2022, 01:29:34 PM
Hi,
I want to raise an alarm in case an outdated Office dll exists.
The path of the file is
C:\Program Files (x86)\Common Files\microsoft shared\OFFICE11\mso.dll

At the moment I use this File.Count DCI:
File.Count("C:\Program Files (x86)\Common Files\microsoft shared\OFFICE11","mso.dll",0,0)

As long as the OFFICE11 folder exists this works fine, but if the folder doesn't exist it returns an error.

I can't count recursive from  \microsoft shared\  for mso.dll as there is a OFFICE16 folder below it containing a mso.dll as well.

Regards

Stefan
Title: Re: Best way to check if a file exists
Post by: Filipp Sudanov on November 16, 2022, 02:36:20 PM
You can make script DCI and process errors inside the script, e.g.:

s = $node->readAgentParameter("File.Size(/tmp/1/1.txt)");
if (s != NULL) return 1; else return 0;

s != NULL condition can be actually simplified:
s = $node->readAgentParameter("File.Size(/tmp/1/1.txt)");
if (s) return 1; else return 0;


I've used File.Size, as it is simpler and has just one parameter, but you can use File.Count as well.

P.S. Instead of script DCI you can use internal DCI with metric "Dummy" and have all the scripting in transformation script.
Title: Re: Best way to check if a file exists
Post by: Victor Kirhenshtein on November 21, 2022, 10:12:01 AM
For version 4.3 we have also added new metric File.Type, which returns 0 if file does not exist, 1 if it is a directory, 2 if it is a device, and 3 if it is a regular file.

Best regards,
Victor