-1

I am not asking how to check if a file exists or how to check if a file is in a specific directory level. Rather I want to know how to check if an existing file is anywhere underneath a specified directory.

Obviously if a file is a direct child of a directory that is easy to check. But what I want to be able to do is efficiently check if an existing file is in a directory including any possible subdirectory. I'm using this in an Android project where I am keeping fine grain control over my cache and I want a utility method to check if a file I may be manipulating is in my cache folder.

Example:

   cache dir
     / \
   dir  file1  
    / \  
file2  file3  

isCacheFile(file2) should return true

Currently I have a method that does it like so

private static final File cacheDir = AssetManager.getInstance().getCacheDir(); // Not android.content.res.AssetManager
private static final String cacheDirName = cacheDir.getAbsolutePath();

public static boolean isCacheFile(File f) {
    if (!f.exists()) return false;
    return f.getAbsolutePath().startsWith(cacheDirName);
}

However, I am inclined to believe there is a better way to do this. Does anyone have any suggestions?

user2967314
  • 13
  • 1
  • 1
  • 3
  • Check this link http://stackoverflow.com/questions/11220678/checking-if-file-exists-in-a-specific-directory – Sushant Tambare Dec 23 '14 at 10:50
  • http://stackoverflow.com/questions/16237950/android-check-if-file-exists-without-creating-a-new-one – Mousa Jafari Dec 23 '14 at 10:52
  • I'm asking about checking if a file is in any subdirectory of a specified directory. Those answers work for checking they are in the imminent directory, but not necessarily a subdirectory. The assumption is the file already exist, and I want to efficiently check if it is somewhere in the cache folder – user2967314 Dec 23 '14 at 10:55
  • Do you mean that you have a complete path to a file, and you want to know if that path is indirectly inside a particular folder? – khelwood Dec 23 '14 at 10:55
  • Yes basically that. If there's a better way to do it then what I have been doing it – user2967314 Dec 23 '14 at 10:56
  • It smells recursion... if you don't find a library to do that for you, eg. apach commons, then you have to do it manually. The algorithm would involve recursevly searching for the specified file, if the file is found, it would mean it exist in any of your subdirectories, if not, then the file does not exist. – Andy Res Dec 23 '14 at 10:56
  • your question doesn't specify what exactly do you want either correct it or stop down voting other answers that are perfectly fine according to your question – Ravinder Bhandari Dec 23 '14 at 10:57
  • I haven't down voted anyone m8 – user2967314 Dec 23 '14 at 10:58

4 Answers4

0

If you have a known path (in the form of File f), and you want to know if it is inside a particular folder (in the form of File cacheDir), you could simply traverse the chain of parent folders of your file and see if you meet the one you are looking for.

Like this:

public static boolean isCacheFile(File f) {
    while (f.getParentDir()!=null) {
       f = f.getParentDir();
       if (f.equals(cacheDir)) {
           return true;
       }
    }
    return false;
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • This is a solid answer, avoids unnecessarily checking all files like the other answers, and probably the way to go. – user2967314 Dec 23 '14 at 11:03
0
boolean isContains(File directory){
File[] contents = directory.listFiles();
if (contents != null) {
    for(int i = 0; i < contents.length; i++){
        if(contents[i].isDirectory())
            isContains(contents[i]);
        else if(contents[i].getName().equals(*your_file_name*))
            return true;
    }
}
return false;
}
grig
  • 848
  • 6
  • 15
0

You could do it by recursion, by calling if the file exists in each sub-directory.

first check if the file exists in the root directory.

boolean exist = new File(rootDirectory, temp).exists();

then if the file wasn't in the root directory, then list all the files and call the method again in the sub-directoy files recursionally until you find the file or there are no more sub-directories.

Salah
  • 8,567
  • 3
  • 26
  • 43
0
public String getPathFromFileName(String dirToStart,Sring fileName){

File f = new File(dirToStart);

File[] list = f.listFiles();

String s="null";

for(int i=0;i<list.length;i++){
    if(list[i].isFile()){
        //is a file

        if(fileName.equals(list[i])){
            s=dirToStart+"/"+fileName;
            break;
        }
    }else{
        //is a directory search further.
        getPathFromFileName(dirToStart+list[i]);
    }
  }

 return s;

}

call this method by passing the parent directory name and the file name to search in subdirectories.

you check the return value if it is not equal to "null", then the files path is returned.

varun
  • 1,473
  • 1
  • 9
  • 15