Announcement

Collapse
No announcement yet.

Custom code in tranformer

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Custom code in tranformer

    I have a channel that does certain things, I would like to add some code in the transfer where, it reads a local directory, gets a list of any .txt files and then checks to see if the modified date is greater then 25 seconds. From there I then set a channel map variable where in the detestation I check if that variable is greater then 0.

    Trying to get this code to work but getting errors

    Code:
    var directory_location = 'c:/temp/';
    var allFiles = [];
    var allFiles = new File(directory_location).listFiles();
        for (i=0; i<allFiles.Length; i++) {
        if (allFiles[i].endsWith(".txt")) {
           var date_lastModified = new Date(allFiles[i]).lastModified());
           var msBetween = new Date().getTime() - date_lastModified.getTime();
           if (msBetween > 25000 ) {
             channelMap.put('allFiles',1);
           }
       }
    }
    Errors I am getting is, it does like like new File and second cannot find function listFiles in object c:/temp

  • #2
    Hello,

    on the File Reader Connector you are able to do that easily!
    It is mandatory doing that on a Transformer step?

    Best Regards,
    Alex Neiva
    Best Regards,
    Alex Neiva

    Comment


    • #3
      Yes It is mandatory to do it in the transformer as this customize channel has alot of lines of codes in the transformer to do other things, on the source, the connector type is changed to JavaScript Reader which I set a polling frequency of 60000ms so it just runs the channel every minute.

      Comment


      • #4
        I'm not expert but it may be the call for new File

        Here is some code I use to check if a file exists.

        Code:
        if (new java.io.File(‘/path/to/file’).exists()){
        
        }

        Comment


        • #5
          Like Aaron shows, you need to use the full package name when accessing most Java classes. There are a few alternatives, but this is the most common way to do it. Once you fix those, you've still got a couple typos that you probably would have found. You can also make your task a little easier by using FileUtils, which is included with mirth. Try this:

          Code:
          var directory_location = 'c:/temp/';
          var txt_files = org.apache.commons.io.FileUtils.listFiles(new java.io.File(directory_location), ['txt'], false);
          // Iterator() is a Rhino function that converts a Java Iterable or Iterator to a JavaScript Iterator
          for (var file in Iterator(txt_files)) {
          	var date_lastModified = new java.util.Date(file.lastModified());
          	var msBetween = new java.util.Date().getTime() - date_lastModified.getTime();
          	// logger.info(file.getName() + ':' + msBetween);
          	if (msBetween > 25000 ) {
          		channelMap.put('allFiles',1);
          		// no need to check all if you find one since the flag is already set
          		break;
          	}
          }

          Comment


          • #6
            that seemed to work, thank you

            Comment

            Working...
            X