How to create dynamic filename

Hi KNIME Reporting Team,

I'm new to this product and quite happy with the product, but little disappointed for the lack of detailed documentation.

I want to create a dynamic file name for XLS Writer like - <filename> + <current timestamp>.xlsx but I don't know how to achive this.

Help is very much appreciated!

Thanks & Regards

Hi JoJo,

you can dynamically assign a filename to an XLS Writer node using Flow Variables. Assuming you found out how to use Flow Variable, your challenge may be to generate the timestamp. Here is how to do it:

  • KNIME has a Time Generator node. If you setup this node to generate one single row and tick "Use execution time as starting time", as a result you will get a single row table with the execution date/time.
  • With it, you can use the Date FIeld Extractor and Time Field Extractor nodes to extract Year, Month, Day, Hours, Minutes, Seconds and Milliseconds.
  • Add then a String Manipulation node to convert these values from int to string (use the string() function) and concatenate them all (using the join() function).

The result will be a timestamp string you can useĀ to appendĀ to your <filename> (use another String Manipulation node) to generate the unique filename you need.

Hope this makes sense, otherwise feel free to ask again here.

Cheers,
Marco.

1 Like

HiĀ marco_ghislanzoni,

Thanks for the reply.

I'm still in trouble. Could you please show me a way to do it (like a workflow sketch).

Thanks & Regards,

JoJo

Very simple example attached, using a Java edit variable node to create the timestamp.

// Enter your code here:

//Replace this with whatever - possibly an incoming flow variable
String fname="C:\\Path\\to\\my\\output\\filename";

// See https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html for format codes
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy_HH-mm-ss.SSS");
	
out_timestamp = sdf.format(new Date());
out_filename = fname+"_"+out_timestamp+".xls";

Hopefully that is reasonably modifiable to what you want.

Check out https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html for the format options.Ā  'new Date()' gives the current date/time.

Steve

HiĀ s.roughley,

Thanks for the help. It's very much appreciated.

Best Regards,

JoJo

Ā 

And here an equivalent, even if using many more nodes, pure KNIME solution.

Cheers,
Marco.

1 Like

Hi marco_ghislanzoni,

Ā 

Thanks for the solution, that too was great.

Best Regards,

JoJo

Hi,

this one really helped me! I was curious if its possible to change the code a little bit to get the date of one week earlier.

To make this more clear: Every week I create an csv output with the dynamic filename and te next week I need this file as input to run the workflow again. I would like to use this code for writing as well as reading the csv.

Thanks in advance!

Probably your easiest way is to modify the snippet using the new java Date/Time using this example:

import java.util.Date;

Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_MONTH, -7);
Date sevenDaysAgo = cal.getTime();

from https://stackoverflow.com/a/4902675/6076839

(Actually, you should probably use Calendar.getInstance())

Steve

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.