Sidebar

How do I create a backup of a file by copying to another folder?

0 votes
430 views
asked Jun 1, 2015 by marc-m-9513 (570 points)
I am trying to create a backup of a file in case I need to resend the document. I am using the following and keep getting "[line: 38] [func: 3 line: 29] IOException: Unable to create the file: E:\Dictaphone\repository\biscom\APOGEE20150601113843273  in <Unknown Source> at line number 38
 

// create output filename
var filename = messageCache.getValue('Location') + messageCache.getValue('Route') + messageCache.getValue('DOCUMENTID');
var file = new java.io.File(filename);

// write bytes out to file
var out = new java.io.FileOutputStream(file, false);
var bytes = outputStream.toByteArray();
for (var i=0 ; i < bytes.length ; i++) {
out.write(bytes[i]);
}
out.close();
// log file saved
qie.info('saved to ' + filename);

function copy() {
var copyfile, newpath;
copyfile = new qie.ActiveXObject('scripting.FileSystemObject');
copyfile.CopyFile (filename, 'E:\\Dictaphone\\repository\\biscom\\' + messageCache.getValue('Route') + messageCache.getValue('DOCUMENTID'), true);
}

1 Answer

0 votes

If the file contents is the message object, the simplest way would be to add an additional File Destination and specify the path.

If you do need to construct the path, and copy a separate file from one location to another, I would use the apache FileUtils like this:

var FileUtils = org.apache.commons.io.FileUtils;
File = java.io.File;
 
// create output filename
var fromFilename = "C:" + File.separator + "Qvera" + File.separator + "In" + File.separator + "one.data";
var fromFile = new java.io.File(fromFilename);
 
// create output filename
var toFilename = "C:" + File.separator + "Qvera" + File.separator + "Out" + File.separator + "one.data";
var toFile = new java.io.File(toFilename);
 
// Copy the file to the new destination
if (fromFile.exists()) {
   try {
      FileUtils.copyFile(fromFile, toFile);
      qie.info('copied to ' + toFilename);
   } catch (err) {
      qie.warn('Caught error trying to copy file: ' + err);
   }
} else {
   qie.info(fromFilename + ' does not exist.');
}

You can remove the extra qie.info logging once it is working the way you want.  Also note, it is a good idea to use the File.separator value, which will use a \ for windows and a / for linux.

answered Jun 3, 2015 by mike-r-7535 (13,830 points)
...