I am trying to filter downloaded files from FTP. I want to exclude from downloading list files with an extenssion ".doc", for instance. But my regex "^(?!.DOC$)" doesnt work. Can you help me with this trouble?
@Bean
@ServiceActivator(inputChannel = "fetch")
public FtpOutboundGateway gateway() {
FtpOutboundGateway gateway = new FtpOutboundGateway(sessionFactory(), "mget", "payload");
gateway.setOptions("-R");
gateway.setLocalDirectory(new File("/local/dir"));
gateway.setFilter(new FtpSimplePatternFileListFilter("^(?!.DOC$)"));
gateway.setRemoteDirectoryExpression(new LiteralExpression("/remote/dir"));
return gateway;
}
It's the regex that's the problem. Yours is only going to ever reject 4-character long strings starting with any character and ending with "DOC" (case-sensitive). For example, filenames
aDOC,5DOC,.DOCetc are the only ones that will be rejected.This will do what you want:
Let's walk through what that does:
.*matches any sequence of characters of any length.(\.doc|\.DOC)will look for.docor.DOCat the end of a filename. Note the\., which means a literal.character. By default.means "any character" in regex, so it has to get escaped with a backslash.(?<! ... )right before the$is a negative lookbehind at the end of the line, so it only rejects files that have that extension.