Skip to Content

How to read Filename in sender SFTP adapter in SAP HCI

Hi Experts,

I have a scenario where the Sender is SFTP. I need to read the value of a filename.

Now i able read full filename by using CamelFileName in Content Modifier.

Filename:

ABC_20171215_46270.51_API1_hhmmss.txt

I want to extract only API1, after third underscore read the data.

Anyone can please tell me, how can be we do this.

Regards,

Thouheed.

1.jpg (27.1 kB)
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Dec 27, 2017 at 12:59 PM

    Hi Mohammad

    When you have the filename, all that's left is a bit of string handling.

    The java.lang.String class has a lastIndexOf method, that you can use to find the index of the last occurrence a given character. There's an overloaded version that takes an index to begin searching from. Using those two methods, you can find the index of the last and the second to last underscore, and then use the substring method to extract your target string. Here's some Groovy code that does exactly that:

    def fileName = 'ABC_20171215_46270.51_API1_hhmmss.txt'
    def lastUnderscoreIndex = fileName.lastIndexOf('_')
    println "lastUnderScoreIndex = ${lastUnderscoreIndex}"
    def secondToLastUnderscoreIndex = fileName.lastIndexOf('_', lastUnderscoreIndex - 1)
    println "secondToLastUnderscoreIndex = ${secondToLastUnderscoreIndex}"
    def targetString = fileName.substring(secondToLastUnderscoreIndex + 1, lastUnderscoreIndex)
    println "targetString = ${targetString}"

    Regards,

    Morten

    Add comment
    10|10000 characters needed characters exceeded

    • Hello Touheed,

      Looks like this question is not related to above mentioned query.If its not related please create a new post.

      And it would be always great if you can up vote the helpful answers or accept the answers which are helped you before marking the question to closure .

      Regards,

      Sriprasad Shivaram Bhat

  • Dec 29, 2017 at 06:37 AM

    Thanks Morten,

    Same groovy script i have used, but some changes has been done now it's working fine.

    import com.sap.gateway.ip.core.customdev.util.Message;
    import java.util.HashMap;
    def Message processData(Message message) {
    
    def secondToLastUnderscoreIndex = fileName.lastIndexOf('_', lastUnderscoreIndex - 1)
    println "secondToLastUnderscoreIndex = ${secondToLastUnderscoreIndex}"
    def targetString = fileName.substring(secondToLastUnderscoreIndex + 1, lastUnderscoreIndex)
    println "targetString = ${targetString}"*/
    
    
    def body = message.getBody(java.lang.String) as String;
    def lastUnderscoreIndex = body.lastIndexOf('_');
    def secondToLastUnderscoreIndex = body.lastIndexOf('_', lastUnderscoreIndex - 1);
    def targetString = body.substring(secondToLastUnderscoreIndex + 1, lastUnderscoreIndex);
    message.setBody(targetString.toString());
    
    
    return message;
    }
    
    

    Thanks and Regrads,

    Md.Thouheed

    Add comment
    10|10000 characters needed characters exceeded