IBM webMethods Hybrid Integration

IBM webMethods Hybrid Integration

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
Expand all | Collapse all

ListFiles using JCraft

  • 1.  ListFiles using JCraft

    Posted Mon July 11, 2011 04:21 PM

    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.


    #webMethods
    #Integration-Server-and-ESB
    #Flow-and-Java-services


  • 2.  RE: ListFiles using JCraft

    Posted Mon July 11, 2011 05:22 PM

    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();

    #Integration-Server-and-ESB
    #Flow-and-Java-services
    #webMethods