Skip to main content
Announcements
SYSTEM MAINTENANCE: Thurs., Sept. 19, 1 AM ET, Platform will be unavailable for approx. 60 minutes.
cancel
Showing results for 
Search instead for 
Did you mean: 
asafrian1700127126
Contributor III
Contributor III

Get Current Date using tSetGlobalVar

Hi, I have the following job. I want to add the latest date in the zip file like Book1yyy_20240710 but there is an error.

Archive File : ("D:/alfy/test_talend/hasilnya/result_zip/")+((String)globalMap.get("tFileList_5_CURRENT_FILE")).replaceAll("\\.xlsx$", "").replaceAll(" \\.pptx$", "")+"_"+((Date)globalMap.get("curdate"))+".zip"

asafrian1700127126_0-1720598212125.png

asafrian1700127126_1-1720598240255.png

asafrian1700127126_2-1720598268275.png

asafrian1700127126_3-1720598319788.png

asafrian1700127126_0-1720598619849.png

 

Error :

Starting job test_zip at 14:54 10/07/2024.
[statistics] connecting to socket on port 3646
[statistics] connected
Exception in component tFileArchive_4 (test_zip)
java.io.FileNotFoundException: D:\alfy\test_talend\hasilnya\result_zip\Book1yyy_Wed Jul 10 14:54:23 WIB 2024.zip (The filename, directory name, or volume label syntax is incorrect)
at java.base/java.io.FileOutputStream.open0(Native Method)
at java.base/java.io.FileOutputStream.open(FileOutputStream.java:295)
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:236)
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:124)
at com.talend.compress.zip.Zip.doZip1(Zip.java:248)
at com.talend.compress.zip.Zip.doZip(Zip.java:179)
at local_project.test_zip_0_1.test_zip.tFileList_5Process(test_zip.java:542)
at local_project.test_zip_0_1.test_zip.tSetGlobalVar_1Process(test_zip.java:739)
at local_project.test_zip_0_1.test_zip.runJobInTOS(test_zip.java:985)
at local_project.test_zip_0_1.test_zip.main(test_zip.java:823)
[statistics] disconnected
Job test_zip ended at 14:54 10/07/2024. [Exit code  = 1]
 

How do I get just the date without errors and do I need other components?

Labels (2)
1 Solution

Accepted Solutions
Shicong_Hong
Support
Support

Hello

Use a tJava instead of tSetGlobalVar component to get the current date and format it to a string value. 

String currentDate=TalendDate.formatDate("yyyyMMdd", TalendDate.getCurrentDate());

globalMap.put("currentDate",currentDate);

Use another tJava after tFileList to build the output file path, the job looks like:

tJava_1
|onsubjobok

tFlieList--iterate--tJava_2--oncompoentok--tFileArchive

on tJava_2:

String filename=((String)globalMap.get("tFileList_1_CURRENT_FILE"));

String file=filename.substring(0, filename.lastIndexOf(".")); //get the filename without extension

globalMap.put("file", file);

on tFileArchive component, set the Archive file field as:

"D:/output_file_path/"+(String)globalMap.get("file")+"_"+(String)globalMap.get("currentDate")+".zip"

Set the filemask as:

((String)globalMap.get("tFileList_1_CURRENT_FILE"));

// The tFileArchive component will archive each file to a separated zip file.

Hope it helps!

Regards

Shicong

 

 

 

 

View solution in original post

3 Replies
Shicong_Hong
Support
Support

Hello

Use a tJava instead of tSetGlobalVar component to get the current date and format it to a string value. 

String currentDate=TalendDate.formatDate("yyyyMMdd", TalendDate.getCurrentDate());

globalMap.put("currentDate",currentDate);

Use another tJava after tFileList to build the output file path, the job looks like:

tJava_1
|onsubjobok

tFlieList--iterate--tJava_2--oncompoentok--tFileArchive

on tJava_2:

String filename=((String)globalMap.get("tFileList_1_CURRENT_FILE"));

String file=filename.substring(0, filename.lastIndexOf(".")); //get the filename without extension

globalMap.put("file", file);

on tFileArchive component, set the Archive file field as:

"D:/output_file_path/"+(String)globalMap.get("file")+"_"+(String)globalMap.get("currentDate")+".zip"

Set the filemask as:

((String)globalMap.get("tFileList_1_CURRENT_FILE"));

// The tFileArchive component will archive each file to a separated zip file.

Hope it helps!

Regards

Shicong

 

 

 

 

asafrian1700127126
Contributor III
Contributor III
Author

it worked, very helpful, thank you
so if we use "globalMap.put" it will automatically become a global variable without having to use the tSetGlobalVar component? and if using tSetGlobalVar component then no need to use "globalMap.put" right?

Shicong_Hong
Support
Support

Yes, you can use 'globalMap.put' to declare a global variable without having to use the tSetGlobalVar component, this makes it easy for you to define global variables in code snippets on tJava/tJavaFlex/tJavaRow.