Does anyone know a way to find a file on a users system without knowing the path name, just the directory name? I know it's probably some recursive function, and if it is, could you kind of get me started on it, or point me in the right direction?
Printable View
Does anyone know a way to find a file on a users system without knowing the path name, just the directory name? I know it's probably some recursive function, and if it is, could you kind of get me started on it, or point me in the right direction?
I don't know if it would have to be recursive or not. Perhaps if the directory contained other directories.
This might help.
Code:import java.io.*;
public class DirectoryLister{
public static void main(String[] args){
File entry;
if(args.length == 0){
System.out.println("Please specify a directory for scanning");
return;
}else {
entry = new File(args[0]);
}
if(!entry.exists()){
System.out.println("Unable to process request " + entry.getPath() + " is nonexistent ");
return;
}
listDirectory(entry);
}
public static void listDirectory(File entry){
try{
if(!entry.exists()){
System.out.println(entry.getName() + "not Found");
}
if(entry.isFile()){
System.out.println(entry.getCanonicalPath());
}else if (entry.isDirectory()){
System.out.println(entry.getPath());
String[] filename = entry.list();
if(filename == null) return;
for(int i = 0; i < filename.length; i++){
File item = new File(entry.getPath(), filename[i]);
// list it by a recursive call
listDirectory(item);
}
}
} catch(IOException e){System.out.print("Error:" + e);}
}
}
You would have to chew through the directory structure and get a list of all the files in each nested directory ie. String[] filename = entry.list(); Then loop through the array returned and compare each string with the name of the file that you are looking for.
That was pretty awesome. The only thing is, lets say I don't know the starting directory, how would I do that? I tried something like this, but it doesn't work...
I was hoping I could get the root directory from this, and then use the return val as the root dir.Code:
public File PathStart()
{
for (char dirs='a'; dirs >= 'z'; dirs++)
{
File path = new File(dirs + ":/");
if (path.isDirectory())
{
startingPath = path;
System.out.println(startingPath.toString());
}
}
return startingPath;
}
There's two problems with the code:
1) lol it doesn't work
2) the user could have cd drives, usb or whatever
If it makes it easier, I can explain the program I'm TRYING to make.
Sorry, startingPath was a File Object declared as a global variable.
You have the "/" backwards. It should be ":\" for windows platforms. You should also get in the habbit of writing platform independent code when the code accesses the underlying file system of the os.
Ok. I changed it, but if that's not a good way to do it, how could I write code that is platform independent and starts at the root directory?
Windows has no root directory, so that's kinda hard to do. It's something really tricky, and I haven't yet seen a satisfactory solution.
Take a look at the static members of the File class like File.separator they are pretty easy to use.
Code:import java.io.File;
String s = new String("C:" + File.separator);
Would the root directory on a one dirve system just be C:\?
Yes, but how many one-drive systems are there? I mean, every computer has at least a floppy drive or a CD drive.
Then there's network shares - don't get me started...
On the other hand, there's the Windows Shell namespace, which has a single root, the desktop. The Shell namespace is what you browse through with Explorer.
I'm currently trying to write a C++ library to capture this stuff. It's ... interesting.
Yeah you have a point. :lol:
I wonder if we can somehow use this:
Do you think this would work, or is this just gonna give the directory that the program is currently running from?Code:String path = System.getProperty("user.dir");
the user.dir property you mention points to the hom directory of the current tuser on linux, or the my Documents on windows, you could use this as a starting point, and step backwards through the parents untill you reach a null, thenyou'd know where your 'root' was, however as dilenger said on windows you don't have a root as such c:\ is only the root of the c drive, where as on linux everything is mapped in the one filesystem.. i don't think Java offers the granularity of the actual underlying system, instead showing them as C: D: etc..
i've had a thought, you could get a FileSystemView, this is what makes up the core of the JFileChooser, there is a method on it that is called getRoots() this returns a File[], if this does what it says on the tin it should give you a starting point for a recursive search of your file system!
It does indeed work, but only for local drives.