Exporting CSV file with Date and Time in the file name

Hi All,
Thank you all for your help!!

We have a need to export data tables to a CSV file - since we need to do that almost every hour - each and every time we export a file we need to change it’s name to be unique.

Currently file name is “mydata”.
I would like to change the export CSV routine that it would export a CSV file with the time and date in the filename. that way we will be able to download the files with ONE click at a time.

I use attributes in projects that hold the Site and location of the project itself. I use a template and I would also like to add the Site and Location cusomes to the file name - that way I will be able to download all databases in ONE folder with no need to create a folder for each Site and/or project.

ExportCSV to have a file witth Site+Location+“DATA”+Date+Time.csv

Is that doable?
Please help me to have that file formatted as mentioned above exported.

Thanks in advanced
Arye

The filename parameter for system.dataset.exportCSV is a string, which you can build any way you want. As an example of how to build the filename using the current date and time:

[code]import datetime

site = ‘mysite’
location = ‘mylocation’
now = datetime.datetime.now()
mydate = now.strftime(’%Y%b%d’)
mytime = now.strftime(’%H%M%S’)

csvName = site+’’+location+‘DATA’+mydate+’’+mytime+".csv"
print csvName
[/code]

Naturally, you’d tie the site and location variables to your attributes, format the date and time the way you want (see the Python docs for more formatting symbols), and then pass the string to exportCSV instead of printing it.

Thanks for your help.

I have tried it as follows:
[color=#FF80FF]import [/color]datetime
site = [color=#8040FF]"[Client]Customer"[/color] [color=#BFBFFF]#should hold the Customer name[/color]
location = [color=#8040FF]""[Client]Site"[/color] [color=#BFBFFF]#should hold the Site name[/color]
now = datetime.datetime.now()
mydate = now.strftime([color=#8040FF]’%Y%b%d’[/color])
mytime = now.strftime([color=#8040FF]’%H%M%S’[/color])

csvName =site+’’+location+‘DATA’+mydate+’’+mytime+".csv"
table = event.source.parent.getComponent([color=#8040FF]“Table”[/color]) # Get a reference to the table
system.dataset.exportCSV(csvName, 1, table.data)

The only thin is that i am getting it wrongly.
[color=#FF0080][Client]Customer_[Client]Site[/color]_DATA_2014Mar24_130402.csv

I would have wanted it to get the name of the Site and the name of the Customer at the beginning of the FileName.

Can you please help me with that?
I am using the Client attributes tags to hold the information of the Project using my indirect template.

Thank You!

Try system.tag.read:

site = system.tag.read("[Client]Customer").value #should hold the Customer name
location = system.tag.read("[Client]Site").value #should hold the Site name

You’re telling the script to use a literal value for site and location by putting something in quotes. Instead, you want to tell it to use the value of the tag.

You probably hit the tag icon off to the right of the scripting area and chose your tag. While that seems like the right thing to do, especially when you choose “value” for that tag, it doesn’t work (as you found out). Instead, take the string it gave you ("[Client]Customer"), and use that as the argument for system.tag.read()

Remember that system.tag.read() gives you a Python tuple, with both the value and the quality. So you would ultimately want something like:

location = system.tag.read("[Client]Site").value

To make it more robust, you can add checks for good quality, make sure the tag value has only valid filename characters, etc. But this will get you going.

[edit]
Thanks, Adam, for beating me to it by a hair! :smiley:

Thanks! it works!