|
-
Jan 30th, 2005, 08:06 PM
#1
Thread Starter
Frenzied Member
find a specific directory
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?
-
Jan 30th, 2005, 08:25 PM
#2
Dazed Member
Re: find a specific directory
I don't know if it would have to be recursive or not. Perhaps if the directory contained other directories.
-
Jan 30th, 2005, 08:31 PM
#3
Dazed Member
Re: find a specific directory
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);}
}
}
-
Jan 30th, 2005, 08:36 PM
#4
Dazed Member
Re: find a specific directory
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.
-
Jan 31st, 2005, 03:47 PM
#5
Thread Starter
Frenzied Member
Re: find a specific directory
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...
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;
}
I was hoping I could get the root directory from this, and then use the return val as the root dir.
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.
-
Jan 31st, 2005, 03:51 PM
#6
Thread Starter
Frenzied Member
Re: find a specific directory
Sorry, startingPath was a File Object declared as a global variable.
-
Jan 31st, 2005, 05:01 PM
#7
Dazed Member
Re: find a specific directory
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.
Last edited by Dilenger4; Jan 31st, 2005 at 05:43 PM.
Reason: Im retarded
-
Jan 31st, 2005, 05:10 PM
#8
Thread Starter
Frenzied Member
Re: find a specific directory
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?
-
Jan 31st, 2005, 05:51 PM
#9
Re: find a specific 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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 31st, 2005, 05:53 PM
#10
Dazed Member
Re: find a specific directory
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);
-
Jan 31st, 2005, 05:55 PM
#11
Dazed Member
Re: find a specific directory
Would the root directory on a one dirve system just be C:\?
-
Jan 31st, 2005, 05:56 PM
#12
Re: find a specific directory
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 31st, 2005, 06:00 PM
#13
Dazed Member
Re: find a specific directory
Yeah you have a point.
-
Jan 31st, 2005, 06:09 PM
#14
Thread Starter
Frenzied Member
Re: find a specific directory
I wonder if we can somehow use this:
Code:
String path = System.getProperty("user.dir");
Do you think this would work, or is this just gonna give the directory that the program is currently running from?
-
Feb 1st, 2005, 05:08 AM
#15
Addicted Member
Re: find a specific directory
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..
-
Feb 1st, 2005, 05:19 AM
#16
Addicted Member
Re: find a specific directory
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!
-
Feb 1st, 2005, 06:37 AM
#17
Re: find a specific directory
It does indeed work, but only for local drives.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|