I’m having trouble using ogscript.asyncFTPListFiles. I can successfully connect to the FTP server, but the returned fileList variable is empty. I know the FTP is connecting since I’m monitoring the server. I just used the example code included in the Dashboard Script Palette. I can’t find any documentation on this function in the ogScript reference doc.
Here’s the code I’m using:
/* fileList consists of an array of org.apache.commons.net.ftp.FTPFile */
var fileList;
function callback(success, fileList, exception)
{
if (success) {
ogscript.debug(“FTP success”);
}else{
ogscript.debug(“FTP Fail!”);
}
}
ogscript.asyncFTPListFiles(‘localhost’, ‘21’, ‘username’, ‘password’, ‘’, callback);
ogscript.debug(fileList);
I would verify that you have valid files at the default directory for that username/password or try manually-specifying a path. I have just verified the following code snippet against one of our local FTP servers:
function outputResults(success, files, exception)
{
if (!success)
{
ogscript.debug(“NO SUCCESS”);
return;
}
else if (files != null)
{
/*
* files[i].getName()
* files[i].getTimestamp // returns java.util.Calendar
* files[i].getSize() // returns file size in bytes
* files[i].isFile() // returns true if the file is a File (not a directory)
* files[i].isDirectory() // returns true if the file is a Directory
*/
ogscript.debug(“GOT " + files.length + " FILES”);
for (var i = 0; i < files.length; i++)
{
var jsTime = (new Date(files[i].getTimestamp().getTimeInMillis()));
if (files[i].isDirectory())
{
ogscript.debug("GOT DIRECTORY: " + files[i].getName());
}
else
{
ogscript.debug("GOT FILE: " + files[i].getName() + " " + jsTime);
}
}
}
}
#DashBoard
That works, thanks! I think I wasn’t reading the array properly, so it looked like nothing was getting passed.
#DashBoard
Another question, since it seems asyncFTPListFiles isn’t documented anywhere: Is there a way to recursively list directories? I can successfully list directories and files in the root level, and differentiate between them. I’m having a problem digging into each directory to list those files, too.
Basically I’m putting the directory names into an array, and the file names into another. Eventually I’m trying to populate a drop-down with file names in the format “directory/filename.”
If the directory array length is more than 0, then I do another asyncFTPListFiles and repeat the process. The problem comes from the way I’m looping through all the directories. I’m using a for loop to go through the directory array and open a new FTP connection, using the name from the array as the directory to list.
if (dirList != 0){
for (var a = 0; a < dirList.length; a++){
var currDir = dirList[a];
ogscript.asyncFTPListFiles(FTPip, 21, FTPuser, FTPpass, currDir, outputResults1);
}
}
Since the code is asynchronous, I can’t use a variable in a for loop to mark what second level-directory we’re listing, since by the time the second directory has been listed, the for loop has gone through all the other folders.
for (var i = 0; i < files.length; i++) // files is the array returned by asyncFTPListFiles
{
constraintData.push(currDir + ‘/’ + files[i].getName()); //Push the key/value pair
}
Any thoughts? Thanks again!
#DashBoard