Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Directory listing including al Subdirectories? 1

Status
Not open for further replies.

fenris

Programmer
May 20, 1999
824
CA
I can get a listing of all the files and directories contained within a specific directory. I am wondering what is the best way to get a listing of the contents of all the subdirectories. I am not sure how to go about this problem. I think that some recursion will be necessary. <br><br>Ultimately I want to create a text file that has the files listed in it with there particular attributes. I want to have the text file with each row having the following structure.<br><br>directory path ¦ file name ¦ file Size ¦ date created ¦ etc.<br><br>I am new to java, what would be the best approach to this problem, if I also wanted to be able to sort the text file by file name, if there where 100,000 entires or so. How would I go about this. I am familiar with sorting algorithms and such, but have never tried this before.<br><br>Any help is greatly appreciated.<br><br>fenris<br> <p> fenris<br><a href=mailto:fenris@hotmail.com>fenris@hotmail.com</a><br><a href= > </a><br> I am interested in Mining Software, as well as Genetic Algorithms.
 
Hi!<br><br>Here is a simple example:<br><br>import java.io.*;<br><br>public class Tree {<br>&nbsp;&nbsp;static void processADir(String dir, String[] fl) {<br>&nbsp;&nbsp;&nbsp;&nbsp;File f;<br>&nbsp;&nbsp;&nbsp;&nbsp;int i=0;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;if (fl!=null)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while (i&lt;fl.length) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f=new File(dir, fl<i>);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (f.isDirectory())<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;processADir(f.getPath(), f.list());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else System.out.println(f);&nbsp;&nbsp;// or out to file<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i++;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;public static void main(String[] args) {<br>&nbsp;&nbsp;&nbsp;&nbsp;String startDir=&quot;.&quot;;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;if (args.length==1) { startDir=args[0]; }<br>&nbsp;&nbsp;&nbsp;&nbsp;Tree.processADir(startDir, new File(startDir).list());<br>&nbsp;&nbsp;}<br>}<br><br>Good luck. Bye, Otto.<br>
 
Thanks again Otto, I can't wait to try it out.<br><br><br>fenris <p> fenris<br><a href=mailto:fenris@hotmail.com>fenris@hotmail.com</a><br><a href= > </a><br> I am interested in Mining Software, as well as Genetic Algorithms.
 
I just tried out your code and now I have a good idea on how to do this sort of thing, Thank you very much.<br>I had to modify it slightly to get it to compile. I changed the line: f = new File(dir, fl) to f = new File(dir, fl<i>)<br><br><br>I have another question, how would I store the contents of a directory to an array if it was not known how many elements the array was supposed to hold?<br><br>fenris <p> fenris<br><a href=mailto:fenris@hotmail.com>fenris@hotmail.com</a><br><a href= > </a><br> I am interested in Mining Software, as well as Genetic Algorithms.
 
I see them identical :))).<br>&gt;I had to modify it slightly to get it to compile. I changed the line: f = new File(dir, fl) to f = new File(dir, fl)<br><br>Put the results into a Vector first (addElement()), after process a dir make a new array with the length of Vector, and copy elements into it.<br><br>Bye, Otto.<br>
 
Sorry about that my mistake, it should read:<br><br>f = new File(dir,fl<i>)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>doh!<br><br>thank you for your help, it would have taken me along time to come up with the recursive directory listing.<br><br><br><br> <p> fenris<br><a href=mailto:fenris@hotmail.com>fenris@hotmail.com</a><br><a href= > </a><br> I am interested in Mining Software, as well as Genetic Algorithms.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top