Perform all File IO operations – Code Templates

In this post, I’m creating the code templates that will do one stop solution for all problems that we face in mirth while doing the File based IO operation.

Imagine a case where you need to move the file from one directory to another via Mirth instead of source or destination connector provided by tool.

In that case:
1. first we need to check if the file exists or not?
2. we have to use FileUtil.write function to write that file to destination location
3. we have to delete the file from the source location.

The performance time required to complete this process will be high. and we have to catch exceptions in correct places. To avoid all these troubles I have developed Mirth code template library that will be one stop solution for all these problem.

This library utilizes apache commons FileUtils library. Download that library from this link here. In that link under binaries select commons-io-2.6-bin.zip if you are using Windows and commons-io-2.6-bin.tar.gz if you are using linux systems.

apache commons fileutils library

Once downloaded navigate to commons-io-2.6 folder and copy the JAR file named commons-io-2.6 alone. You will also find other JAR files along with that, you can ignore them, you have to copy this and place it in your custom-lib folder of Mirth connect installed directory. Once  done, go to Mirth settings tab and click on Reload Resource.

Provide the following codes in your code template library:

Copy Directory To Directory:

function copyDirectoryToDirectory(sourceDirectory, destinationDirectory) {

var srcDirectory = new java.io.File(sourceDirectory);

var destDirectory = new java.io.File(destinationDirectory);

try {

var copyDirectoryToDirectory = new Packages.org.apache.commons.io.FileUtils.copyDirectoryToDirectory(srcDirectory, destDirectory);

} catch (exp) {

logger.debug(exp);

}
}

call from Transformer:

copyDirectoryToDirectory(“C:/Projects/PROJECTS/TEST/Sample Message/PDF-tests/sourcedirectory”,”C:/Projects/PROJECTS/TEST/destinationDirectory”);

Move File To Directory:

function moveFileToDirectory(sourceFileName, destinationDirectoryName) {

var srcFile = new java.io.File(sourceFileName);

var destDir = new java.io.File(destinationDirectoryName);

try {

var moveFileToDirectory = new Packages.org.apache.commons.io.FileUtils.moveFileToDirectory(srcFile, destDir, false);

} catch (exp) {

logger.debug(exp);

}
}

call from Transformer:

moveFileToDirectory(“C:/Projects/PROJECTS/TEST/Sample Message/PDF-tests/test2.pdf”, “C:/Projects/PROJECTS/TEST/Sample Message/”);

Move Directory To Directory:

function moveDirectoryToDirectory(sourceDirectory, destinationDirectory) {

var srcDirectory = new java.io.File(sourceDirectory);

var destDirectory = new java.io.File(destinationDirectory);

try {

var moveDirectoryToDirectory = new Packages.org.apache.commons.io.FileUtils.moveDirectory(srcDirectory, destDirectory);

} catch (exp) {

logger.debug(exp);

}
}

call from Transformer:

moveDirectoryToDirectory(“C:/Projects/PROJECTS/TEST/Sample Message/PDF-tests/sourcedirectory”, “C:/Projects/PROJECTS/TEST/destinationDirectory”);

Copy File To Directory:

function copyFileToDirectory(sourceFileName, destinationDirectoryName) {

var srcFile = new java.io.File(sourceFileName);

var destDir = new java.io.File(destinationDirectoryName);

try {

var copyFileToDirectory = new Packages.org.apache.commons.io.FileUtils.copyFileToDirectory(srcFile, destDir);

} catch (exp) {

logger.debug(exp);

}

}

call from Transformer:

copyFileToDirectory(“C:/Projects/PROJECTS/TEST/Sample Message/PDF-tests/test.pdf”, “C:/Projects/PROJECTS/TEST/Sample Message/”);

Leave a Comment