I am trying to find a file in the root directory and its subdirectories.
Step1 - find the directory at the specified path. Step 2. If the directory found above is found, find the file in one of the subdirectories.
To do this, I use the code snippet below that searches recursively. Now the problem is how I escape from recursion when it meets both of my 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;
}
}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]" );
}
}
}
Thanks, DK
source
share