0

I'm trying to work out how to process a file, and then copy the file to the /finished directory. If I just leave the 'to' as a directory, it generates file names for me. I want the same file name.

In my processor, I have:

        GenericFile gfile = exchange.getIn().getBody(GenericFile.class);
        exchange.getOut().setBody(gfile);

And in my route, I have, for example:

    <route id="fileLoader" autoStartup="true">
        <from uri="file://{{xls.dir}}?noop=true&amp;antInclude=*xlsx&amp;delay=1000" />
          <to uri="bean:loaderProcessor"  />
          <to uri="file://{{xls.dir}}finished/?fileName=${in.header.CamelFileName}"  />
    </route>

There's all sorts of advise about using '{file:name}' and '{in.header.CamelFileName}' but none of it has worked for me.

(Btw, xls.dir has a / at the end, so that's not a problem)

djb
  • 1,635
  • 3
  • 26
  • 49
  • 1
    have you tried `toD` instead of `to`? If yes, try to add `` to check header is there. – Bedla Jul 12 '18 at 18:19
  • `{in.header.CamelFileName}` will not work Your syntax is wrong. Try `${in.header.CamelFileName}` since you are using the Simple language you will need to use `${}` not `{}`. The file name is there. I have done this a million times. – Namphibian Jul 13 '18 at 00:19
  • 1
    In your processor then do NOT set the OUT message, just leave it alone and Camel will use the existing file message and write it using its name. Then you do not have to set fileName parameter in the to either. – Claus Ibsen Jul 13 '18 at 05:46
  • Claus's suggestion worked, thanks. Namphibian: ${} is what I wrote, and it doesn't work with fileName, or if I just use it without fileName, it says 'must not contain dynamic expressions'. – djb Jul 13 '18 at 08:17
  • @Claus Ibsen, how would I delete the original file after it copies to the new directory? – djb Jul 16 '18 at 16:05
  • Read the docs, there is a delete=true option you can set – Claus Ibsen Jul 16 '18 at 18:52
  • That would delete it as soon as it left the from (), would it not? I'm asking about deleting it after the route completes the copy. I had it on delete before, but I need to keep the file in the folder until the route completes – djb Jul 17 '18 at 19:40
  • @ClausIbsen: delete=true deletes the file, yes, but I need it to not delete the file if the route throws an exception during bean processing. The docs aren't really clear on this use case. – djb Aug 24 '18 at 11:05
  • The file is only deleted if the message was processed succesfully. In case of failure the file stays as-is and will re-attempt on next poll. You can use moveFailed to move the file to a special folder etc. – Claus Ibsen Aug 24 '18 at 12:21

1 Answers1

0

${headers.CamelFileName} will give you the file name that is picked for processing. Besides this, we have many other header properties that are available. You can follow the Camel Official documentation for additional header properties.

Refer to below URL for the documentation : https://camel.apache.org/components/3.15.x/file-component.html#_message_headers

Guru Tata
  • 251
  • 1
  • 3