web stats
Custom code in tranformer - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 02-20-2018, 07:14 AM
Ghosttown Ghosttown is offline
Mirth Newb
 
Join Date: Dec 2017
Posts: 13
Ghosttown is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 02-20-2018, 07:21 AM
AlexNeiva AlexNeiva is offline
Mirth Guru
 
Join Date: Oct 2013
Location: Portugal
Posts: 277
AlexNeiva is on a distinguished road
Default

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
Reply With Quote
  #3  
Old 02-20-2018, 07:40 AM
Ghosttown Ghosttown is offline
Mirth Newb
 
Join Date: Dec 2017
Posts: 13
Ghosttown is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 02-20-2018, 09:37 AM
appsbyaaron's Avatar
appsbyaaron appsbyaaron is offline
Mirth Guru
 
Join Date: Nov 2011
Location: Florida, USA
Posts: 377
appsbyaaron is on a distinguished road
Default

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()){

}
Reply With Quote
  #5  
Old 02-21-2018, 06:29 PM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 964
agermano is on a distinguished road
Default

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;
	}
}
Reply With Quote
  #6  
Old 02-22-2018, 05:12 AM
Ghosttown Ghosttown is offline
Mirth Newb
 
Join Date: Dec 2017
Posts: 13
Ghosttown is on a distinguished road
Default

that seemed to work, thank you
Reply With Quote
Reply

Tags
transformer

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 07:36 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Mirth Corporation