javascript sftp file name check

mardi 2 décembre 2014

I can connect to the sftp folder but I just need to see if the file exists by the file name. It sounds easy but for some reason I just keep getting errored...







//Create Function.

function loopRun(fileName, filePath2, fileName2) {

//fileName is the location and file on the MIRTH Appliance

//filePath2 is the SFTP directory

//fileName2 is the SFTP file name

logger.info('in the function');

try {

contents1 = FileUtil.read(fileName);



//Get the File Size

contents2 = FileUtil.readBytes(fileName);

kb1 = contents2.length / 1024;



FileUtil.write(goodFile, true, fileName+'\n');



//If the backup file is found then check to see if the SFTP move file is there.

//Connect to SFTP

/* The file checks on all SFTP folders fails because we are not connecting to them. */

logger.info('About to connect');



session.connect();

var channel = session.openChannel('sftp');

channel.connect();

logger.info('Connected');

channel.cd(filePath2); //Go to the folder for the file.

logger.info('Changed path to : '+filePath2);



try {

logger.info('read file : '+fileName2); //never comes up so I know it fails here

var contents3 = FileUtil.read(fileName2);

FileUtil.write(goodFile, true, filePath2+fileName2+'\n');



//Get the File Size

contents4 = FileUtil.readBytes(fileName2);

kb2 = contents4.length / 1024;

logger.info('contents4 : '+contents4);



//If the file sizes are different then make a note of it.

if (kb1 != kb2) {

fileName = fileName + kb1;

fileName2 = fileName2 + kb2;

FileUtil.write(differentFile, true, fileName+'\n');

FileUtil.write(differentFile, true, filePath2+fileName2+'\n\n');

}

}

catch(err) {

logger.info('Err 1');



FileUtil.write(missingFile, true, filePath2+fileName2+'\n');

//Close SFTP Connection

channel.exit();

session.disconnect();

}

}

catch(err) {

logger.info('Err 2');

FileUtil.write(missingFile, true, fileName+'\n');

//Close SFTP Connection

channel.exit();

session.disconnect();

}

//Close SFTP Connection

channel.exit();

session.disconnect();

logger.info('Clean up');



//Clean up

fileName = '';

fileName2 = '';

contents1 = '';

contents2 = '';

contents3 = '';

contents4 = '';



kb1 = 0;

kb2 = 0;

}





javascript sftp file name check

0 commentaires:

Enregistrer un commentaire