Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File search recursively

Tags:

android

I am trying to find a file in a root directory and it's sub-directories.

Step1- Find a directory in a specified path. Step2- If the above directory is found, look for a file in one of it's sub-directory.

For this, I use the below code snippet that search recursively.. Now, the issue here is, how do I break out of the recursion when it meets both of my above requirements..?

 boolean bFileFound = false;
File  fileFound     = null;

private void findFile( File aFile, String sDir ){

    String  filePath = aFile.getAbsolutePath();

    if( aFile.isFile() && filePath.contains( sDir ) ){

              if( aFile.getName().contains( "test2.adv")){
                  Log.d(TAG, "[FILE] " + aFile.getName() );
                  fileFound = aFile;
                  bFileFound = true;
              }

             // return true;
    }else if( aFile.isDirectory() ){

        String sDirName = aFile.getName();
        Log.d(TAG, "[DIR] " + sDirName );

        if( sDirName.contains( sDir ) ){

            Log.d( TAG, "Found the directory..& Absolute Path = " + aFile.getAbsolutePath());
            sDir = sDirName;
        }

        File[] listFiles = aFile.listFiles();

        if( listFiles != null ){

          for( int i = 0; i < listFiles.length; i++ ){

              if(bFileFound)
                    return;

            findFile( listFiles[ i ], sDir );
          }
        }else{

          Log.d( TAG,  " [ACCESS DENIED]" );
        }
    }

   // return null;
}

Thanks, DK

like image 691
codersnet Avatar asked May 14 '12 18:05

codersnet


People also ask

What is a recursive file search?

Alternatively referred to as recursive, recurse is a term used to describe the procedure capable of being repeated. For example, when listing files in a Windows command prompt, you can use the dir /s command to recursively list all files in the current directory and any subdirectories.

What command searches all files recursively?

You can use grep command or find command as follows to search all files for a string or words recursively.

How do you do a recursive search?

To grep All Files in a Directory Recursively, we need to use -R option. When -R options is used, The Linux grep command will search given string in the specified directory and subdirectories inside that directory. If no folder name is given, grep command will search the string inside the current working directory.

How do I recursively search a folder?

Using Find You can also use the find command followed by the target directory and the file you wish to locate. The command will start in the root directory and recursively search all the subdirectories and locate any file with the specified name.


1 Answers

/**
 * Search file a file in a directory. Please comment more here, your method is not that standard.
 * @param file the file / folder where to look our file for.
 * @param sDir a directory that must be in the path of the file to find
 * @param toFind the name of file we are looking for. 
 * @return the file we were looking for. Null if no such file could be found.
 */
private File findFile( File aFile, String sDir, String toFind ){
    if( aFile.isFile() && 
            aFile.getAbsolutePath().contains( sDir ) && 
            aFile.getName().contains( toFind ) ) {
                        return aFile;
        } else if( aFile.isDirectory() ) {
        for( File child : aFile.listFiles() ){
            File found = findFile( child, sDir, toFind );
                    if( found != null ) { 
                        return found;
                    }//if
        }//for
    }//else
   return null;
}//met

Now, pass "test2.adv" as third param when you invoke findFile. That's more interesting than hardcoding it.

Also please note that multiple files could match your search, this function doesn't handle it well, it will return the first one found.

like image 152
Snicolas Avatar answered Sep 22 '22 14:09

Snicolas