Skip to Content
0

How to read Filename in sender SFTP adapter in SAP HCI

Dec 27, 2017 at 07:58 AM

255

avatar image

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)
10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Morten Wittrock Dec 27, 2017 at 12:59 PM
1

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

Show 3 Share
10 |10000 characters needed characters left characters exceeded

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.

0

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

0

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

0
Mohammad Thouheed Dec 29, 2017 at 06:37 AM
0

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

Show 2 Share
10 |10000 characters needed characters left characters exceeded

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.

0

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

2