cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Dynamic Extension Setting for File Name Output in SAP CPI

shin861231
Participant
0 Kudos
1,350

Hello Experts,

I am now trying doing file decompression (Unzip) within SAP CPi. I unzip two files (.txt and .csv) contained in one ZIP file and store them on an FTP server using the FTP Adapter.

In the FTP Adapter settings, there's a place to set the name of the output files (1 txt file and 1 csv file), but how can I dynamically set the "extension" in this case?

 

As an example, this is what I expect.

Input Files' (to be unzipped) Name

> AAA.zip (which contains BBB.txt and CCC.csv)

Output Files

>BBB.txt and CCC.csv

(By the way, when I left the file name setting of FTP adapter blank, then the output files came out as BBB and CCC, which means there was no extension.)

(The attached screenshot shows where the file name is set.)

Thank you.

 

FTP Adatper File Name.jpg

Accepted Solutions (0)

Answers (3)

Answers (3)

Aamir_Suhail
Explorer
0 Kudos

This may sound weird, but some Windows based FTP servers show only the filename (without extension) by default. Please make sure that is not the case.

shin861231
Participant
0 Kudos
Hello, I have checked the blog you provided, but I did not see the part explaining how to set the file name, especially the extension part dynamically. Could you please specify which part I should refer to?
Aamir_Suhail
Explorer
0 Kudos
I just followed the blog and successfully got the proper unzipped files with dynamic extensions. However, one thing that you are doing different is using FTP instead of SFTP. Can you try this setup using an SFTP receiver?
anoukvandenengel
Participant
0 Kudos

Hello,

If I understand you correctly, if you leave the File Name field empty the output files are missing the extension, but can you explain why ${header.CamelFileNameOnly} is not working?

In my test scenario, ${header.CamelFileNameOnly} contains the complete path, including the filename and file extension (i.e. zip_file_name/file_name.file_extension).

Assuming that in your case ${header.CamelFileNameOnly} also only contains the file naming excluding extension, I have the following suggestions:

1: Have you tried using ${file:name}?

2: I recently had the requirement to set the target directory dynamically, which I solved by creating a map (in an exchange property) containing the file name as key and the directory as value. Perhaps something similar would also work for the file extension. Assuming that ${header.CamelFileNameOnly} only contains the the file name without extension, you could implement the following steps:

Step 1: use a Groovy script to fill the map. You can check if one of the headers or properties contains the extension type or check the file content for the correct type.

 

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
    
    // Retrieve properties and headers
    def properties = message.getProperties()
    def headers = message.getHeaders()

    // initiate an empty map
    def extension_map = [:]
    
    // Retrieve the filename from the headers
    def filename = headers.get("CamelFileNameOnly")

    // set the extension
    def extension = // implement logic to get file extension
    
    // If the 'Extensions' property already exists, overwrite the empty map with it
    if (properties.get("Extensions")) {
        extension_map = properties.get("Extensions")
    }

    // Append new key-value pairs
    extension_map[filename] = extension

    // Set the updated map (back) to the exchange property
    message.setProperty("Extensions", extension_map)

    return message;
}

 

Step 2:

In the File Name field, set: ${header.CamelFileNameOnly}.${property.Extensions[${header.CamelFileNameOnly}]}

anoukvandenengel_1-1727423960053.png

I hope that one of these options works for you.

AlexanderApel
Participant
0 Kudos

just leave the filename blank and the original file name will set automatically.