I want to be able to read in all files below a certain directory level and whose names are unkown. I actually know them but don't want to code in 100+ file names. Ideas?
12/10/2005 2:14:46 AM
Haha would involve changing the names of your files but what if you renamed them to something like
file001file002file003
// Process all files and directories under dir public static void visitAllDirsAndFiles(File dir) { process(dir); if (dir.isDirectory()) { String[] children = dir.list(); for (int i=0; i<children.length; i++) { visitAllDirsAndFiles(new File(dir, children[i])); } } } // Process only directories under dir public static void visitAllDirs(File dir) { if (dir.isDirectory()) { process(dir); String[] children = dir.list(); for (int i=0; i<children.length; i++) { visitAllDirs(new File(dir, children[i])); } } } // Process only files under dir public static void visitAllFiles(File dir) { if (dir.isDirectory()) { String[] children = dir.list(); for (int i=0; i<children.length; i++) { visitAllFiles(new File(dir, children[i])); } } else { process(dir); } }
12/10/2005 10:22:09 AM
thanks man, i'll try that out
12/10/2005 2:36:41 PM
yeah, no need to rename anythinggoogle search for recursive directory listing in java should be more than sufficient
12/10/2005 8:55:03 PM
maybe i'm not understanding the question, but couldn't you just use a java File object like this:File myDirectory = new File(directory);File[] files = myDirectory.list();if there are directories within that directory and you want to get those you'd have to loop through the array, check if it is a directory (using isDirectory()), and then get the contents for each one. If you just need the names than you can just set the list to an array of Strings instead of Files like:String[] files = myDirectory.list();Just google "java File" and look at the javadoc for more info
12/10/2005 9:50:02 PM
are you actually a junior in csc/cpe?????wtf
12/10/2005 10:16:31 PM
^^^Doing it recursively can break sometimes ^I was a junior by credit hours my third semester and didn't have many classes under my belt by that point. Please contribute something useful neo, I don't think many of us undertand what you are trying to do. Are you trying to get the names of the files, read the contents of the files into memory, or what? "Below a certain directory" ... are you talking about "subdiretory depth of 5" or "/" or what?
12/11/2005 12:33:32 PM
just use perl
12/11/2005 12:52:58 PM
The question is straightforward and the answer is not that omplicated either. I don't know much java but I've done exactly this in several languages. Heres the process:Define the starting point:1. let "BaseDir" be that point (i.e. "C:\MyApp\loadthesefiles")Create a global array (one that can be accessed from any subroutine) for file names;2. let "FileArray[]" be that arrayCreate a recursive subroutine or function. It should take a directory as an input:3. let "FileList(Directory)" represent that function3.a. this subroutine should open the indicated directory and read its contents (see LittleZZ's post)3.b. loop through the directory's contents checking each item to see if it's a file or a directory3.b.i. if it's a file, append it's path and name to FileAray[]3.b.ii. if it's a directory, have FileList call itself on that directory like "FileAray(files[i])"4. Call FileList(BaseDir)5. Upon completion, FileArray[] will contain every file name under your base directoryJava should have a big enough stack to go at least dozens of directories deep. VB's (ver 6) stack size is 128. Directories can be nested 255 levels deep. If you need o go deeper than stak space allows, this routine (and all recursive routines) can be writen as a flat code (i.e. no function calls). All you need to do in that case is track a queue of directories to be searched in an array and process each directory entirely before moving to the next.
12/11/2005 1:06:52 PM
12/11/2005 1:56:22 PM
This is csc 116 shit.
12/11/2005 3:01:40 PM
^^^^
12/11/2005 3:30:04 PM
System.exec( perl get me tha files);[Edited on December 11, 2005 at 3:31 PM. Reason : dsa]
12/11/2005 3:31:30 PM
^that presupposes perl is on the machine.
12/12/2005 8:51:36 AM