Java tutorial
//package com.java2s; //License from project: Apache License import java.io.File; import java.io.FilenameFilter; public class Main { public static File findParentDirContainerFile(File file, final String fileKey) { if (fileKey.equals(file.getName())) { return file; } File[] guessFileKeys = file.listFiles(new FilenameFilter() { public boolean accept(File dir, String name) { return fileKey.equals(name); } }); if (guessFileKeys.length > 0) { return file; } if (file.getParentFile() != null) { return findParentDir(file.getParentFile(), fileKey); } else { return file.getParentFile(); } } public static File findParentDir(File file, String fileKey) { if (fileKey.equals(file.getName())) { return file; } else if (file.getParentFile() != null) { return findParentDir(file.getParentFile(), fileKey); } else { return file.getParentFile(); } } }