cancel
Showing results for 
Search instead for 
Did you mean: 

Problem wid nested folders.

Former Member
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

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

Answers (5)

Answers (5)

Former Member
0 Kudos

Still Could not find the exact answer for nested folders

Former Member
0 Kudos

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

Former Member
0 Kudos

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

Former Member
0 Kudos

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

Former Member
0 Kudos

Hi both,

thanks for the reply.

@Narendra : I hv used ur jar file n reimport it.but still m getting same error.

@Kishan: m nt gettng calss not found exception. : but now its showing me loading only when i click

on action.

Its like i want d list of folders on WAS.. I think it shud b possible

Thanks n Regards

Khushboo

Former Member
0 Kudos

Hi,

but now its showing me loading only when i click on action

Can you be more specific about the above error you are getting?

Regards

Kishan

Former Member
0 Kudos

Hi,

I means its just keep excecuting the application... like its goin in infinite loop...

n never shows me any output.

Thanks

Khushboo

Former Member
0 Kudos

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

Former Member
0 Kudos

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

Former Member
0 Kudos

Hi Narendra

I hv downloaded this jar file from common apache(commons-io-1.3.2.jar)

n follwed d same steps for jar addition..bt still m gettng d same error...

Regards

Khushboo

Former Member
0 Kudos

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

Former Member
0 Kudos

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

Former Member
0 Kudos

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