1

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;
}
Sneftel
  • 40,271
  • 12
  • 71
  • 104
asker
  • 11
  • 3

1 Answers1

1

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, .DOC etc are the only ones that will be rejected.

This will do what you want:

^.*(?<!(\.doc|\.DOC))$

Let's walk through what that does:

  1. .* matches any sequence of characters of any length.
  2. (\.doc|\.DOC) will look for .doc or .DOC at 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.
  3. Putting the (?<! ... ) right before the $ is a negative lookbehind at the end of the line, so it only rejects files that have that extension.
AmphotericLewisAcid
  • 1,824
  • 9
  • 26