Results 1 to 9 of 9

Thread: Move file to other directory

  1. #1
    Join Date
    Feb 2012
    Posts
    19

    Default Move file to other directory

    Hi all,
    I have a requirement, in which I need to read a file using a <file:inbound-channel-adapter /> and then send the data in the channel. Then change the name of the file as abc.csv to abc.csv.processed and move it to different folder.

    How can I do this using Spring integration?

    Thanks,
    Dev

  2. #2
    Join Date
    Feb 2012
    Posts
    19

    Default

    I am able to complete one part of my requirement, i.e. moving file from one directory to other and also forwarding the data in the channel , by using <file:outbound-gateway/> after the <file:inbound-channel-adapter/>.
    But now I am having one more requirement i.e. if the file is empty, then move it to rejected folder. How can I achieve this requirement? Please help.

    Thanks,
    Dev

  3. #3

    Default

    seems to me that this might be a small problem..
    Last edited by wikes; Mar 26th, 2012 at 06:18 AM.

  4. #4
    Join Date
    Feb 2012
    Posts
    19

    Default

    Quote Originally Posted by wikes View Post
    seems to me that this might be a small problem..
    As I am new to SI, every suggestion will be helpful for me.

    Thanks,
    Dev

  5. #5
    Join Date
    Mar 2010
    Location
    Gtr Philadelphia, PA
    Posts
    2,142

    Default

    Use a <router/> with expression="payload.length() == 0" (this evaluates to File.length() when the payload is a File object). When true, route to rejected channel when false, route to normal channel.
    Gary P. Russell
    Spring Integration Team
    SpringSource, a division of VMware

  6. #6
    Join Date
    Feb 2012
    Posts
    19

    Default

    Quote Originally Posted by Gary Russell View Post
    Use a <router/> with expression="payload.length() == 0" (this evaluates to File.length() when the payload is a File object). When true, route to rejected channel when false, route to normal channel.
    Hi, actually I want to move the file (if empty) from one folder to other, not the message in the channel.
    Using router we can move the message in the channel, what I know.

    Thanks,
    Dev

  7. #7
    Join Date
    Mar 2010
    Location
    Gtr Philadelphia, PA
    Posts
    2,142

    Default

    Right...

    Code:
    <file:inbound-channel-adapter channel="choose"
                                  directory="file:${java.io.tmpdir}/spring-integration-samples/input"
                                  filename-pattern="*.txt">
    	<int:poller id="poller" fixed-delay="5000" max-messages-per-poll="2"/>
    </file:inbound-channel-adapter>
    
    <int:router input-channel="choose" expression="payload.length() == 0">
    	<int:mapping value="true" channel="reject" />
    	<int:mapping value="false" channel="filesIn" />
    </int:router>
    
    <int:channel id="reject" />
    
    <int:service-activator input-channel="reject" output-channel="stdout" 
    	expression="payload.renameTo(new java.io.File('/tmp/rejected/' + payload.name))" />
    
    <int-stream:stdout-channel-adapter id="stdout" append-newline="true" />
    This just puts the result of the rename to stdout - you could add a test to make sure it's true. Or, use groovy or java instead of a simple SpEL expression in the <service-activator/> that renames the file.
    Gary P. Russell
    Spring Integration Team
    SpringSource, a division of VMware

  8. #8
    Join Date
    Oct 2011
    Location
    Mumbai, India
    Posts
    213

    Default

    You can have a file inbound adapter with a filter downstream with the expression Gary gave (expression="payload.length() == 0").
    The output channel of the filter can be an input to an file outbound channel adapter which writes to a specific directory. You may have the delete-source-files attribute set to true.

    Have a look at this section for file adapters and this for message filters.

  9. #9
    Join Date
    Feb 2012
    Posts
    19

    Default

    Thanks Gary and Amol.

    I used mixed solution suggested by you. I used file inbound adapter with a router downstream with the expression Gary gave (expression="payload.length() == 0"). Then I used rejected channel of the filter as an input to an file outbound channel adapter which writes to a specific directory also I used delete-source-files attribute set to true as suggested by Amol.

    Thank you once again both of you.
    Thanks,
    Dev

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •