Skip to Content
avatar image
Former Member

Problem wid nested folders.

Hi All,

I'm trying to get all folder located on C:\ of server.

..lets say thre are 4 folders in C:\

thn I'm gettng those 4 folder of server..

but i want to do it all folder's name which thse 4 folders are containg on C:\

I'm tryng to use nested looping ,but m getting "null pointer exception" on statement:

rootfolder.listFiles()<i>.listFiles().length ;

using this line m trying to retrieve no of folders which are contained by secondary folder .

I hope m nt being too complicated.

Do any one hv any idea regarding it

hlpful posts will be appriciated.

Thanks n Regards

Khushboo

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

6 Answers

  • Best Answer
    avatar image
    Former Member
    Jan 09, 2009 at 07:33 AM

    Hi Khushboo,

    I agree it only returns directories directly under the initial directory.

    You need to write the logic in a recursive function to get all the folders and sub-folders.

    Kind Regards,

    Nitin

    PS: Updated sample code - hope it helps you

    File directory = new File("directoryName");

    listDirectories (directory);

    void listDirectories(File directory)

    {

    // This filter only returns directories

    FileFilter fileFilter = new FileFilter() {

    public boolean accept(File file) {

    return file.isDirectory();

    }};

    File[] files = directory.listFiles(fileFilter);

    if(files.size() == 0)

    System.out.println("Folder is = " + directory.getAbsolutePath());

    else

    for(int i=0; i<files.size(); i++)

    listDirectories(files<i>);

    }

    Edited by: Nitin Jain on Jan 9, 2009 9:10 AM

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jan 08, 2009 at 04:54 AM

    Hi Khushboo,

    The error shows it is not getting the class FileUtils.

    have you used the correct jar file.And added the jar files to your projects library.

    i mean right click on your project name->Properties->java build path ->library->add external jar and then add the jar file .

    Regards

    Narendra

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Hi Khushboo,

      Copy the jar file to the lib folder located inside your Web Dynpro project in the Navigator tab.

      Rebuild->Re-deploy and run. It should work fine.

      Regards

      Kishan

  • avatar image
    Former Member
    Jan 07, 2009 at 01:42 PM

    Hi Khushboo,

    you can use the below code it will give you desired result.

    i have already impleted it. so i can clarify all your doubts.

    For this you need to use commons-io-1.3.2.jar which can be easily available over internet

    import org.apache.commons.io.FileUtils;
    int i=0;
    			File root = new File("C:/Xlf");
    			File file = null;
    
    			String[] extensions = { "xlf" };
    			boolean recursive = true;
    
    			//
    			// Finds files within a root directory and optionally its
    			// subdirectories which match an array of extensions. When the
    			// extensions is null all files will be returned.
    			//
    			// This method will returns matched file as java.io.File
    			//
    			Collection files = FileUtils.listFiles(root, extensions, recursive);
    			ArrayList arrayList = new ArrayList();
    			
    
    			for (Iterator iterator = files.iterator(); iterator.hasNext();) {
    				String fileName = "file";
    				i++;
    				fileName = fileName +i;
    				arrayList.add(fileName);
    				file = (File) iterator.next();
    				System.out.println("File are = " + file.getAbsolutePath());
    }

    Regards

    Narendra

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi Narendra,

      I hv used ur code as it is... just made one change

      File root = new File("D:/usr");

      as this folder exists on WAS

      but m gettng one error

      java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils

      i hv added the .jar file which u hv mentioned..

      Regards

      Khushboo

  • avatar image
    Former Member
    Jan 08, 2009 at 05:07 AM

    Hi khushboo,

    once remove the import statement and then try to reimport it, build your project gain and try.

    because its working fine fir me.

    Regrads

    Narendra

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      HI,

      If you are working with a DC,

      1:You need to create an 'External Library' project , include the .jar file into it , build and deploy into server.

      2: Then add this project in WebDynpro references to your DC.

      Also here is sample code to access folders in server at some location...

      Use this if useful..

      File f = new File("c:
      ");

      File[] arr = f.listFiles();

      int len = arr.length;

      for(int i=0;i<len;i++)

      {

      if(arr<i>.isDirectory())

      if(!(arr<i>.isHidden()))

      wdComponentAPI.getMessageManager().reportSuccess(" "+arr<i>.getName());

      }

      Regards

      Narayana

  • avatar image
    Former Member
    Jan 08, 2009 at 11:15 AM

    Hi Khushboo,

    As I understand, the said code will print the absolute path of all the files under the usr/... folder (You would have given 'null' as the extension parameter of the files for the same).

    This will take some time as huge no. of files will be there.

    Secondly, System.out will print in the Console view of the NWDS.

    Thirdly, as i get your requirement is to print ONLY FOLDERS (not all the files), so you will need to make small changes to the code to get the appropriate collection (size of this will be very less).

    Sample code - http://www.exampledepot.com/egs/java.io/GetFiles.html (search-engines)

    Kind Regards,

    Nitin

    Edited by: Nitin Jain on Jan 8, 2009 12:19 PM

    Edited by: Nitin Jain on Jan 8, 2009 12:42 PM

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi

      @Nitin:the link which u have given to me search folders in particular drive..it wont search folder in folders..

      i mean all directories n subdirectories.

      @Laxmi: ur code is for file search in c:/

      my requirement is difrrent

      anything for folders only?

      Thnaks

      Khushboo

  • avatar image
    Former Member
    Mar 20, 2009 at 04:44 AM

    Still Could not find the exact answer for nested folders

    Add comment
    10|10000 characters needed characters exceeded