cancel
Showing results for 
Search instead for 
Did you mean: 

How to read Filename in sender SFTP adapter in SAP HCI

former_member302452
Participant
0 Kudos

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.

Accepted Solutions (0)

Answers (2)

Answers (2)

MortenWittrock
Active Contributor

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

former_member302452
Participant
0 Kudos

Hi Morten,

We have a input data like below in that we need to read first to second dot(.) data.

abc_31012018_1000.00_123_xyz_20180131072155.txt Processing Failed with error code 36005 and with reason Multi currency transactions not allowed.Error in record number 1.

I have to read second dot to end of data like below.

txt Processing Failed with error code 36005 and with reason Multi currency transactions not allowed.Error in record number 1.

how to archive it in groovy script.

Regards,

Thouheed.

MortenWittrock
Active Contributor
0 Kudos

Hi Thouheed

I'm not completely sure that I understand the issue, but it sounds to me like you should be able to reuse the same code to solve it.

Regards,

Morten

Sriprasadsbhat
Active Contributor
0 Kudos

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

former_member302452
Participant
0 Kudos

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

MortenWittrock
Active Contributor
0 Kudos

Hi Mohammad. Glad it was of use. Please keep in mind that answers must be explicitly accepted in the new community site. Closing and accepting are two separate actions. Regards, Morten.

MortenWittrock
Active Contributor

Oh, and you don't need the println statements. That was just to show the data, when running from the command line.