Re: Search for a file on hard drive?
Rafael Schleuss <[email protected]>
| Newsgroups | gmane.comp.java.sun.servlet |
|---|---|
| Message-ID | <[email protected]> |
Try this .....
----------------------------------------------------------------------------------
import java.io.*;
class teste
{
public static void main( String args[] )
{
if( args != null && args.length > 0 )
{
if( args[0] != null && args[1] != null)
{
if( findFile( new File( args[0] ), args[1] ) <= 0 )
{
System.out.println("File not found!");
}
}
else
{
System.out.println("Parameter not found!");
}
}
else
{
System.out.println("Parameter not found!");
}
}
public static int findFile( File pDir, String word )
{
int count = 0;
File[] arrFile = pDir.listFiles();
File tmp;
// First look into this folder
for (int i=0; i<arrFile.length; i++)
{
tmp = arrFile[i];
if ( tmp.getName().equalsIgnoreCase(word))
{
System.out.println(tmp);
count++;
}
}
//now look into sub folders
for (int i=0; i<arrFile.length; i++)
{
tmp = arrFile[i];
if( tmp.isDirectory() )
{
findFile( tmp, word );
}
}
return count;
}
}
----------------------------------------------------------------------------------
On Thu, 2003-04-17 at 02:05, Ramchandra wrote:
> Hi,
>
> There is no perticular method for searching the files in folders and
> the sub forlders of it.
>
> Usualy all programmers start searching from a perticular folder and
> then start searching into its sub folders. For doing the same
> programmaticaly in Java U hv to give the following codec within a loop.
> Use isFile() method of File class which returns "True" if the name specified
> is a file and "False" if it is a folder.... If it is false just replace
> the Folder name in File classe's constructor with the name specified and so
> on..... It all depends on how do U manage the loop... The code is as
> follow;
>
> File file = new File("C:\\");
> String [] files = file.list();
> for (int i=0; i<files.length; i++) {
> if (files[i].endsWith(".exe")) {
> System.out.println(files[i]);
> }
> }
>
> Cheers,
> Ramachandra B.S.
> Software Engineer
> Conpudyne Winfosystems Ltd.(CWLGLOBAL)
> Bangalore.
>
> ___________________________________________________________________________
> To unsubscribe, send email to [email protected] and include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
--
Rafael Schleuss
Java Developer
[email protected]