The problem you are facing is that when you search, if you come across a file that does not match the target file, you cause an error. You cannot throw an error through a recursive search without a helper method, you can do something similar instead.
This is completely untested, but should theoretically work.
private FSElement getTargetFromName (FSElement root, String targetName) {
Iterator<FSElement> children = root.iterator();
while (children.hasNext()) {
FSElement child = children.next();
if (child.getName().equals(targetName)) {
return child;
} else if (child.isDirectory()) {
FSElement searched = getTargetFromName(child, targetName);
if (searched != null) {
return searched;
}
}
}
return null;
}
, , , :
public FSElement getTargetFromName (String targetName) throws CommandNotExecutedException {
FSElement file = getTargetFromName(localroot, targetName);
if (file == null) {
throw new CommandNotExecutedException("Destination file/dir not found");
}
return file;
}