Name Excel file with last day of last month

Hello guys,

I would like to save some data from a worklow, the name of the file should always contain the last day of the last month:

e.g. if executed today → name of the file “calulation_28.02.2022.xlsx”

I think I need some date variable here but dont know excactly how to put things together.

Can someone help?

Thank you a lot
Regards

@RapplK you might want to combine these approaches:

1 Like

Hi @RapplK , something like this could do:

When working with Dates, or rather manipulating Dates, it’s best to work with the Dates nodes rather than manually moving days.

The logic I used to get the last day of previous month is to simply take current date minus the current day. So if today is 2022-03-14, the current day is 14, and if you do 2022-03-14 - 14 days, you get the last day of the previous month.

Elaborating the workflow:
First, I create a variable where I can add the template for the file name. This will also allow you to change the template as you will without having to figure out where to make the changes in the “code”. You just need to change the template and the filename will adjust.

The template is defined as a variable in the Variable creator as follows:
image
calulation_dd.MM.yyyy.xlsx

where:
dd: double digits day
MM: double digits month
yyyy: 4-digits year

You can move them around, or change “calculation_” for something else, the file name will adjust.

I use the Create Date&Time Range to get the current date - it’s the usual practice to get the current date, and I get this as current date:
image

After manipulation, I get this as last day of last month:
image

After applying this value to the template and converting to a variable, I get this:
image

At this point, you have to now convert this String variable to a Path variable, which you will then be able to use in your Excel Writer. To do this, we use the String to Path (Variable):

So, I want to convert the String variable “filename”, in my demo I’m generating a path that’s relative to the workflow data folder (so it will point to the workflow’s data folder), and it will add suffix “_location” to the new Path variable. You may choose other locations if you want.

We check the variable, and we see this:
image

You can see the variable “filename_location” was created and it’s of type Path.

Now in your Excel Writer, you can use this new variable. Click on the Variable button:

You will get this pop-up. Check the box in the “Use Variable” section, and choose the variable from the dropdown:
image

Click OK, and you should see this message at the bottom of the Excel Writer:

And this is it.

As you can see, the data was saved as an Excel with the expected file name:
image

If you want to test other dates to make sure that the workflow works, you can just do so by playing around in the Create Date&Time Range:

This is what we would get as current date if it ran on 2022-02-14:
image

And you can see the values of the variables for last day of previous month and file name for the test date:
image

And similarly if you are running in January, it knows how to get Dec 31 of the previous year.
Current date:
image

Generated variables:
image

Here’s the workflow: Name Excel file with last day of last month.knwf (35.7 KB)

3 Likes

wow, I’m speechless. Thank you a lot @bruno29a

also @mlauber71

2 Likes

Dear @bruno29a,

one more question:

“So, I want to convert the String variable “filename”, in my demo I’m generating a path that’s relative to the workflow data folder (so it will point to the workflow’s data folder), and it will add suffix “_location” to the new Path variable. You may choose other locations if you want.

I would like to choose another file location: E.g. C:\Users\z000001\Desktop\Test_Adstar

I Think I have to change the node “string to path” to “Local File System”

and also add the path here??

But somehow its not working yet.

Thank you again for advice :slight_smile:

Hi @RapplK , you can modify the filename variable by prefixing the value with “C:\Users\z000001\Desktop\Test_Adstar”. This should be done before the String To Path node.

You may need to escape the slashes. You can do this via the Variable Expression node or the String Manipulation (Variable) node.

And you are correct, you would use the Local File System in your String to Path node.

1 Like

It worked, thank you.

It was possible even with slashes

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