I have a folder with this structure
mainFolder
--Sub1
--File .scl
--File .awl
--Other files
--Sub2
--Files
--Sub3
--Sub4
I want to copy it to another location but i want the Sub3 to be avoided and (depending from the situation) some file from the Sub1
Here is an extract from what i did so far:
FileUtils.copyDirectory(srcDir, dstDir, new FileFilter() {
public boolean accept(File pathname) {
// We don't want 'Sub3' folder to be imported
// + look at the settings to decide if some format needs to be
// excluded
String[] ignoreList= new String[]{
!Settings.getSiemensOptionAWL() ? ".awl":"uselessStringWilNeverBeFound",
!Settings.getSiemensOptionSCL() ? ".scl":"uselessStringWilNeverBeFound",
"Sub3"
};
return !(ignoreFile(pathname, ignoreList) && pathname
.isDirectory());
}
}, true);
public static boolean ignoreFile(File file, String[] ignoreList) {
for (final String ignoreStr : ignoreList)
if (file.getAbsolutePath().contains(ignoreStr))
return true;
return false;
}
Apparently it seams to work. But i think is a very ugly solution….
Does anyone knows a better way?
P.S:
of course Settings.getSiemensOptionAWL() is just boolean function taht return my decision
The other options suggested here are good, however another alternative is to nest multiple simpler FileFilters together (which may be overkill, of course!)
Then simply combine short, concise FileFilters for the Sub3 case, the .scl and the .awl case. The example FailFastFileFilter I’ve shown above would let you specify null as one of the filters (so you could use inline if statements to determine whether particular FileFilters are applied)
For the sake of completion, here’s a general idea of how I’d implement the child filters for the Sub1 cases and the Sub3 case.
First, a filter to excluding files with a particular extension within a directory:
Then to exclude a directory:
Setting up the FailFastFileFilter would then look something like: