on 03-03-2016 10:34 AM
Hi Team,
I have files in the source directory with the names as :
ATOSS_IMP_ZEITARTEN_20130712_141301.txt.zip
B1234561.TXT_20151208-125416-787.zip
PSA-201512.TXT.zip
BAV-Doppelversorgung_201512.xls.zip
I need to pick the files and remove the extension.zip from all the files and also the timestamp from files which have them using Dynamic Configuration in UDF.Timestamp will always be after _ and before the actual extension of the filename as highlighted above.
Please help me with the code for the same.
Required O/p for above files is :
ATOSS_IMP_ZEITARTEN.txt
B1234561.TXT
PSA-201512.TXT
BAV-Doppelversorgung.xls
PFB the working code for no extensions,2 character extensions and 4 character extensions.:
public class testStr {
public static void main(String[] args)
{
String a="";
String destFileName=null;
String fileNameWithoutZip= a.substring(0,a.lastIndexOf("."));// no second . then o/p is -1 (-1+1=0)
int dot=fileNameWithoutZip.lastIndexOf(".")+1;
int length=fileNameWithoutZip.length();
if (dot==0)
{
destFileName= fileNameWithoutZip.substring(0,length);
System.out.println("no extension");
}
else if((length-dot)==3)
{
destFileName=fileNameWithoutZip;
System.out.println("if1");
}
else
{
if ((length-dot)==4)
{
int dotNew=dot+4;
destFileName= fileNameWithoutZip.substring(0,dotNew);
System.out.println("if2");
}
else
{
if ((length-dot)==24)
{
int dotNew=dot+4;
destFileName= fileNameWithoutZip.substring(0,dotNew);
System.out.println("if3");
}
else
{
int dotNew=dot+3;
destFileName= fileNameWithoutZip.substring(0,dotNew);
System.out.println("else");
}
}
}
System.out.println(destFileName);
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The updated requirement is :
Original File Names | Required File Name |
ATOSS_IMP_ZEITARTEN_20130712_141301.txt.zip | ATOSS_IMP_ZEITARTEN_20130712_141301.txt |
B1234561.TXT_20151208-125416-787.zip | B1234561.TXT |
PSA-201512.TXT.zip | PSA-201512.TXT |
BAV-Doppelversorgung_201512.xls.zip | BAV-Doppelversorgung_201512.xls |
After .txt/.xml/.csv or any other extension we will have the timestamp as highlighted above.
First we have to remove .zip
Then check for the timestamp,if present then remove the same or else do nothing.
Please share your inputs for the same.
Thanks
Rajat
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi again,
lets see if the below works
DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");
String sourceFileName = conf.get(key);
String destFileName=null;
String fileNameWithoutZip= sourceFileName.substring(0,sourceFilleName.lastIndexOf("."));
int dot=fileNameWithoutZip.lastIndexOf(".");
int length=fileNameWithoutZip.length();
if((length-dot)==3)
{
destFileName=fileNameWithoutZip;
}
else
{
int dotNew=dot+3;
destFileName= fileNameWithoutZip.substring(0,dotNew);
}
DynamicConfigurationKey key1= DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");
key1.put(destFileName);
don't put smiley in code !
Regards,
Vikas
Getting an error while compiling:
Resolved the error by using this :
DynamicConfigurationKey key1= DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");
conf.put(key1,destFileName);
return "";
But not getting what is required.
Plus we can have any extension file name like .idif or anything.
May be we have to look at using regex.
Hi Vikas,
Thank you for the help.
I have used the below code with no input to UDF:
DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");
String sourceFileName = conf.get(key);
String destFileName=null;
String fileNameWithoutZip= sourceFileName.substring(0,sourceFileName.lastIndexOf("."));
int dot=fileNameWithoutZip.lastIndexOf(".");
int length=fileNameWithoutZip.length();
if((length-dot)==3)
{
destFileName=fileNameWithoutZip;
}
else
{
int dotNew=dot+3;
destFileName= fileNameWithoutZip.substring(0,dotNew);
}
DynamicConfigurationKey key1= DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");
conf.put(key1,destFileName);
return "";
Error:
Runtime exception when processing target-field mapping /ns0:MT_Source; root message: Exception:[java.lang.StringIndexOutOfBoundsException: String index out of range: -1] in class com.sap.xi.tf._
Hi Vikas,
The below code works for all files names that have extension as 3 characters like .csv,.txt.xls.dat:
public class testStr {
public static void main(String[] args)
{
String destFileName=null;
String fileNameWithoutZip= a.substring(0,a.lastIndexOf("."));
int dot=fileNameWithoutZip.lastIndexOf(".")+1 ;
int length=fileNameWithoutZip.length();
if((length-dot)==3)
{
destFileName=fileNameWithoutZip;
System.out.println("if");
}
else
{
int dotNew=dot+3;
destFileName= fileNameWithoutZip.substring(0,dotNew);
System.out.println("else");
}
System.out.println(destFileName);
}
Tried examples and worked:
1.ATOSS_IMP_ZEITARTEN_20130712_141301.txt.zip
2.B1234561.TXT_20151208-125416-787.zip
3.PSA-201512.TXT.zip
4.BAV-Doppelversorgung_201512.xls.zip
Can you suggest the changes in the code for file extensions of 4 characters like ".idif" ".jpeg"
Sample filenames being:
1. SAP_PEOPLE_TDG_ORG.ldif_20160112-153106-297.zip expected : SAP_PEOPLE_TDG_ORG.ldif
2. SAP_PEOPLE_TDG_ORG_20160112-153106-297.ldif.zip expected :SAP_PEOPLE_TDG_ORG_20160112-153106-297.ldif
Some changes in the requirement will post the updated one in sometime.
I am trying to find a pattern to write a UDF and will require help for the same.
Thanks
Rajat
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi rajat,
try this
DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");
String sourceFileName = conf.get(key);
String fileNameWithoutDate= sourceFileName.substring(0,sourceFilleName.lastIndexOf("_"));
String destFileName=fileNameWithourDate+".txt";
DynamicConfigurationKey key1= DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");
key1.put(destFileName);
Regards,
Vikas
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
File names are in random pattern so difficult to parse.
Although we have the liberty of using Message mapping but looks difficult to write a UDF for the same as _ can be anywhere in the file name and file actual file extension like .txt/.xls can be before or after the timestamp in the file name.
User | Count |
---|---|
83 | |
10 | |
10 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.