Sidebar

How can I monitor the number of files in a folder?

+1 vote
303 views
asked Feb 28, 2017 by michael-h-5027 (14,350 points)
I want to know how many files have been added to a folder on my computer today.

1 Answer

0 votes

You can create a channel with a source of "Script" to get a count of the files in your folder.

var fileDirectory = new java.io.File("C:\\HL7\\In\\");

var listFiles = fileDirectory.listFiles();
var todaysFileCount = 0;

for (var i=0 ; i < listFiles.length ; i++) {
   var file = listFiles[i];
   if (!file.isDirectory() &&
      !file.isHidden() &&
      file.length() > 0) {
      var todaysDate = qie.formatDate('ddMMyyyy');
      var lastModDate = qie.formatDate('ddMMyyyy', new Date(file.lastModified()));
      if (StringUtils.equals(todaysDate, lastModDate)) {
         todaysFileCount++;
      }
   }
}

qie.addInboundMessage(todaysFileCount, 'fileCount');

 

If you wanted to get an error when the count is higher than a specified amount you could create a custom mapping that would throw an error if the count was too high.

var value = source.getNode("/");

if (value > 12) {
   throw "The 'in' folder has " + value + " files pending!";
}

answered Feb 28, 2017 by michael-h-5027 (14,350 points)
...