import java.util.HashMap; import java.io.File; import java.io.FileWriter; import java.io.IOException; //save this code to listreps.java //compile with >javac -Xlint listreps.java //example run with >java listreps c:/windows //the above command will list recursively all directories under the windows directory //I tested it on my windows directory and it listed more than 30000 sub directories. //So please wait that the program finishes. class rept{ private long cnt=0; private String dirIn=""; private HashMap hm = new HashMap(0,90000); void setDir(String dr){ dirIn=dr.replace("\\","/"); } HashMap getReps(){ long bk=0; cnt=0; String newline = System.getProperty("line.separator"); try{ FileWriter facd = new FileWriter("c:/outdirs/da/da.txt",true);//path of file where directories that could not be opened are listed while(true){ //4 try{ String listf[] = new File(dirIn).list(); int nlistf = listf.length; if(nlistf>0){ //3 FileWriter filrep = new FileWriter("c:/outdirs/testdirs.txt",true);//path of file where directories are listed int flg=0; for(int i=0;i<nlistf;i++){ //2 String pth = dirIn+"/"+listf[i]; if(new File(pth).isDirectory()){ //1 cnt=cnt+1; filrep.write(newline+cnt+","+pth); hm.put(cnt,pth); flg=1; }//end 1 }//end 2 filrep.close(); }//end 3 }catch(NullPointerException ne){ facd.write(newline+"bk="+bk+" - dirIn="+ dirIn); }catch(IOException ie){ }//end catchs(ne,ie) bk=bk+1; if(bk==cnt+1) break; dirIn=(String) hm.get(bk); }//end 4 facd.close(); }catch(IOException ioe){ hm.put(0,"file da not closed!"); }//end catchs(ioe) return hm; }//end getReps long getCntreps(){ return cnt; }//end getCntrep }//end class rept public class listreps{ public static void main(String args[]){ rept rp=new rept(); rp.setDir(args[0]); HashMap ham=rp.getReps(); long cntreps=rp.getCntreps(); System.out.println("Total number of directories="+cntreps); ham.clear(); }//end main }//end class listreps