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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching for a file in the DOS Command 1

Status
Not open for further replies.

DonCL

Programmer
Jul 16, 2001
43
US
Hello, I'm trying to write a program that given the name of a file to the console, will search the file system looking for one or more files that match the provided name, and the output shows the full path to the found file.
What would be the easiest way to do this? I'm rather new to Java. Thanks for any help.
 
DonCL,

Here is something to get you started :

import java.io.File;

public class FileList {
public static void main(String args[]) {
File baseDir = new File("C:/");
File[] files = baseDir.listFiles();
for (int i = 0; i < files.length; i++) {
if (new File((&quot;&quot; +files)).isFile()) {
System.out.println(&quot;File : &quot; +files);
}
else {
System.out.println(&quot;Directory : &quot; +files);
}
}
}
}


If you played around with this class a bit, and added each directory to a Vector or ArrayList, and then recursively called the above method on each directory, you would eventually rip through the entire file system structure (looking for your files as you went).

Have fun !
 
Dunno why the above has gone italic, but its also left out a on the files variable...the 'if' statement line should read :

if (new File((&quot;&quot; +files)).isFile()) {

 
Its done it again. Wierd.

In that line the 'files' variable should be 'files'
 
I'm getting sick of this !!! ...

after the 'files' varaible, there should be a reference to the array subscript of that varaiable, the variable i ...

dunno why it keeps removing it when I post !?????
 
sedj,
thanks for your help. i think it keeps getting italicized because it is reading the as an tgml tag. what you are saying is that it should read:

import java.io.File;

public class FileList {
public static void main(String args[]) {
File baseDir = new File(&quot;C:/&quot;);
File[] files = baseDir.listFiles();
for (int i = 0; i < files.length; i++) {
if (new File((&quot;&quot; +files)).isFile()) {
System.out.println(&quot;File : &quot; +files);
}
else {
System.out.println(&quot;Directory : &quot; +files);
}
}
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top