ListFiles using JCraft

Hi All,

I have a requirement where we login to SFTP Server and lists the filenames and gets them.

We’re username/Id to login to the sftp server using Jcraft. Everything is working fine but the list service is listing the long filenames instead of just filenames.

below is the ls java service.

// pipeline
IDataCursor pipelineCursor = pipeline.getCursor();
ChannelSftp c = (ChannelSftp) IDataUtil.get( pipelineCursor, “ChannelSftp” );
String path = IDataUtil.getString( pipelineCursor, “path” );
String pattern = IDataUtil.getString( pipelineCursor, “pattern” );
int n = Integer.parseInt(IDataUtil.getString( pipelineCursor, “substrpos” ));

pipelineCursor.destroy();
Vector fn = new Vector();
String status = null;

try {
if(path == null && pattern == null) {
path = “.”;
fn = c.ls(path);
status = “0”;
}
if(pattern != null) {
fn = c.ls(pattern);
status = “0”;
}
else {
fn = c.ls(path);
status = “0”;
}
}
catch (Exception e) {
status = "Unable to list the name of the files on the FTP Server. "+e.toString();
}
String filenames = new String[fn.size()];
for(int i=0;i<fn.size();i++) {
filenames[i] = (String)fn.get(i).toString();
}
// pipeline
IDataCursor pipelineCursor_1 = pipeline.getCursor();
IDataUtil.put( pipelineCursor_1, “status”, status );
IDataUtil.put( pipelineCursor_1, “filenames”, filenames );
pipelineCursor_1.destroy();

inputs:

ChannelSftp is an Object.
path
pattern
substrpos

output:

status
filenames is a list.

Can someone please reply back the change so that it lists only filenames not the long filenames.

Thanks,
David.

It looks like the problem is here :

The “fn.get(i)” should give you a LsEntry object.
The “toString” of this class gives you the fullfilename.
You have to use getFileName() from LsEntry.

not tested:

filenames[i] = ((LsEntry)(fn.get(i)).getFileName();