Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finding a folder or a file

 
Kelly Harris
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a need to be able to search a folder structure for a file or a folder. I looked at Class File. I could probably use this to find what I need but I am looking for a possibly better solution.
thanks
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might help:

change "/" for "C:\" if you're under windoze
hope it helps
 
Kelly Harris
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Harold A. Gim´┐Żnez
I ended up
<blockquote><font size="1" face="Verdana, Arial">code:
<pre>
/** ------------------------------------------------------------
findFolder
find a specific folder in a path?
------------------------------------------------------------
input:
path:Starting path to search for folder
folder:The folder name to process.
@return
the full path to the specified folder w/o folder
------------------------------------------------------------ */
public String findFolder( String path, String folder)
{
String returnValue = null;
File f = new File(path);
String [] s;
s = f.list();
int counter;

// search this current folder before searchins sub folders
for (counter=0; counter < s.length; counter++)
{
if ( s[counter].compareToIgnoreCase( folder) == 0)
{
returnValue = path;
break;
} // if
//System.out.println( s [counter].toString());
} // for
// Search any sub folders.
if (returnValue == null)
for (counter=0; counter < s.length; counter++)
{
File test = new File( path + s[counter]);
if ( test.isDirectory() == true)
{
returnValue = findFolder( path + s[counter] + '\\', folder);
if (returnValue != null)
break;
} // if
//System.out.println( s [counter].toString());
} // for
return returnValue;
} // findFolder

</pre><hr>
</blockquote>
I was hoping that there was an already built in function to do a search. I am sure that I probably missed something.
 
Kelly Harris
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry all, I exepcted I could do some HTML code here. Oops.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic