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

Java service to extract folders from subdirectories

  • 1.  Java service to extract folders from subdirectories

    Posted Tue December 11, 2012 02:28 PM

    Hi,
    I’m trying to extract files/folders from directories/subdirectories and put everything in a list. I made it work in Java. Here’s the code snippet i have tried so far.

    In Shared tab -
    SOURCE

    
    public static void Extract(String directoryName)
    { 
    try{
    File directory = new File(directoryName); 
    
    //get all the files from a directory
    File[] fList = directory.listFiles();
    
    //for (File file : fList)
    for (int i = 0; i < fList.length; i++)
    {
    if (fList[i].isFile()){                
    } else if (fList[i].isDirectory())
    {
    //Extract(file.getAbsolutePath());
    dirList[i].add(directory.getAbsolutePath());
    i++;
    }
    }
    }
    
    catch(Exception exception)
    {
    //throw new ServiceException("Internal Server Error - Files names could not be retrieved");
    }
    
    }
    

    Code

    IDataCursor pipelineCursor = pipeline.getCursor();
    
    String    dirPath = IDataUtil.getString( pipelineCursor, "dirPath" );
    pipelineCursor.destroy();
    
    ArrayList<Object> dirList=new ArrayList<Object>();
    Extract(dirPath);
    
    // pipeline
    IDataCursor pipelineCursor_1 = pipeline.getCursor();
    //String[]    dirList = new String[];
    //dirList[] = "dirList";
    IDataUtil.put( pipelineCursor_1, "dirList", dirList);
    pipelineCursor_1.destroy();
    
    

    In the source, what im trying to do is, if its a directory im trying to call the same function again. but im not able to get the list of folders in the directories/subdirectories.

    Please help me out in this.

    Thanks


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


  • 2.  RE: Java service to extract folders from subdirectories

    Posted Tue December 11, 2012 03:33 PM

    Try this,

    Shared:

    public static List<String> extract(String directoryName, List<String> outFiles){
    
    File directory=new File(directoryName);
    File[] fileList=directory.listFiles();
    String outFileName;
    for(int i=0;i<fileList.length;i++){
    if(fileList[i].isFile()){
    outFileName=fileList[i].toString();
    outFiles.add(outFileName);
    }
    else{
    outFileName=fileList[i].toString();
    outFiles.add(outFileName);
    extract(outFileName,outFiles);                
    }            
    }
    return outFiles;
    }

    IS Code:

    IDataCursor pipelineCursor = pipeline.getCursor();
    String    dirPath = IDataUtil.getString( pipelineCursor, "dirPath" );
    pipelineCursor.destroy();
    
    List<String> output=new ArrayList<String>();
    
    List<String> tempPlaceHolder=new ArrayList<String>();
    output=extract(dirPath,tempPlaceHolder);
    
    IDataCursor pipelineCursor_1 = pipeline.getCursor();
    IDataUtil.put( pipelineCursor_1, "dirList", output.toArray(new String[output.size()]));
    

    Cheers,
    Akshith


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


  • 3.  RE: Java service to extract folders from subdirectories

    Posted Tue December 11, 2012 04:17 PM

    Hey Akshith,
    Thank you so much. It is working fine.:slight_smile:

    Regards,
    Monish


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


  • 4.  RE: Java service to extract folders from subdirectories

    Posted Tue December 11, 2012 04:30 PM

    Glad could help! You were on right track with the requirement about using recursive function call for directory names.

    Cheers,
    Akshith


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