How to call 'FTP receive'

Post Reply
_olq
Member
Posts: 36
Joined: Fri Aug 23, 2013 10:21 am
Location: Poland

How to call 'FTP receive'

Post by _olq »

Has anyone an idea how to call the 'FTP Receive' based on the previously received an XML file with path to the file I want to download?

Thanks in advance
Aleksander
Aleksander
dkelly
TOP CONTRIBUTOR
Posts: 628
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

Re: How to call 'FTP receive'

Post by dkelly »

The "FTP Receive" configurator does not accept incoming connections. You will have to call a separate FTP program using "Execute Command" or a script.
_olq
Member
Posts: 36
Joined: Fri Aug 23, 2013 10:21 am
Location: Poland

Re: How to call 'FTP receive'

Post by _olq »

Thanks Dwight,
What do you mean by script. From what I know Java Script does not support FTP protocol.
Can you give a hint?
Aleksander
dkelly
TOP CONTRIBUTOR
Posts: 628
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

Re: How to call 'FTP receive'

Post by dkelly »

Code: Select all

// FTPget - get file from FTP server
// 
// Developed by Dwight Kelly <dkelly@apago.com>
// Copyright 2015 - Apago, Inc. -- All rights reserved
//

// Switch logging & output levels
const cLogAssert = -2;
const cLogDebug = -1;
const cLogInfo = 1;
const cLogWarning = 2;
const cLogError = 3;
const cLogProgress = 5;

function jobArrived( s : Switch, job : Job )
{
	var theServer = s.getPropertyValue("server");
	if (theServer.length == 0) {
		job.fail("FTPget: No Server specified!");
		return;
	}
	var thePath = s.getPropertyValue("path");
	if (thePath.length == 0) {
		job.fail("FTPget: No path specified!");
		return;
	}
	var username = s.getPropertyValue("username");
	var password = s.getPropertyValue("password");
	
	var f = new File(thePath);
	var newFileName = job.createPathWithName(f.name, false);
	delete f;

	var argc = 0;
	var args = new Array();
	args[argc++] = "ftp";
	args[argc++] = "ftp://"+username+":"+password+"@"+theServer+"/"+thePath;
	args[argc++] = "-o"; args[argc++] = newFileName;
	
	var exitStatus = Process.execute(args);
	if (exitStatus == 0) {
		job.sendToData(3, newFileName);
	} else {
		job.log(cLogError, "FTPget: download failed, " + Process.stdout);
		job.sendToData(3, job.getPath());
	}
}
_olq
Member
Posts: 36
Joined: Fri Aug 23, 2013 10:21 am
Location: Poland

Re: How to call 'FTP receive'

Post by _olq »

Thanks Dwight,

everything seems ok but I get the error "Error in line 22 of the script: Error. Trying to access undefined member 'length'".
Error relates to "thePath" where I write the name of folder on the server.
When I replace real data to model "ftp://username:password@theServer/thePath" and I put it in browser, everything is OK.

I do not know what I'm doing wrong?
Aleksander
dkelly
TOP CONTRIBUTOR
Posts: 628
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

Re: How to call 'FTP receive'

Post by dkelly »

You have to add the following properties in Scripter:
  • server
  • path
  • username
  • password
_olq
Member
Posts: 36
Joined: Fri Aug 23, 2013 10:21 am
Location: Poland

Re: How to call 'FTP receive'

Post by _olq »

I did like this, I just changed my login details :)

Code: Select all

// FTPget - get file from FTP server
// 
// Developed by Dwight Kelly <dkelly@apago.com>
// Copyright 2015 - Apago, Inc. -- All rights reserved
//

// Switch logging & output levels
const cLogAssert = -2;
const cLogDebug = -1;
const cLogInfo = 1;
const cLogWarning = 2;
const cLogError = 3;
const cLogProgress = 5;

function jobArrived( s : Switch, job : Job )
{
   var theServer = s.getPropertyValue("##.###.###.##");
   if (theServer.length == 0) {
      job.fail("FTPget: No Server specified!");
      return;
   }
   var thePath = s.getPropertyValue("FILES");
   if (thePath.length == 0) {
      job.fail("FTPget: No path specified!");
      return;
   }
   var username = s.getPropertyValue("######");
   var password = s.getPropertyValue("######");
   
   var f = new File(thePath);
   var newFileName = job.createPathWithName(f.name, false);
   delete f;

   var argc = 0;
   var args = new Array();
   args[argc++] = "ftp";
   args[argc++] = "ftp://"+username+":"+password+"@"+theServer+"/"+thePath;
   args[argc++] = "-o"; args[argc++] = newFileName;
   
   var exitStatus = Process.execute(args);
   if (exitStatus == 0) {
      job.sendToData(3, newFileName);
   } else {
      job.log(cLogError, "FTPget: download failed, " + Process.stdout);
      job.sendToData(3, job.getPath());
   }
}
Aleksander
dkelly
TOP CONTRIBUTOR
Posts: 628
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

Re: How to call 'FTP receive'

Post by dkelly »

Here's the complete script element

https://www.dropbox.com/s/jykn52cjef8zo ... cript?dl=0

Dwight Kelly <dkelly.at.apago.com>
bens
Member
Posts: 130
Joined: Thu Mar 03, 2011 10:13 am

Re: How to call 'FTP receive'

Post by bens »

Just a note: I believe the Environment class's download() function also supports simple ftp, so you can omit the Process invocation with

Code: Select all

s.download( "ftp://"+username+":"+password+"@"+theServer+"/"+thePath );
_olq
Member
Posts: 36
Joined: Fri Aug 23, 2013 10:21 am
Location: Poland

Re: How to call 'FTP receive'

Post by _olq »

Thanks to all, nobody said it will be easy :)

I think the problem is Windows.
I found that the only effective way to call the service ftp.exe is through Batch file or VBScript with Batch file.
But I'm not sure this is a good way.

I think I’ll look for a different solution.
First download all files using the 'FTP receive', then from location where the downloaded files are, I choose needed files based on Metadata.

What do you think?
Aleksander
Post Reply