Inserting Current Date into Filename via Modelbuilder

15775
16
02-04-2011 05:46 AM
JeffPickles
New Contributor III
Is there a way to programmatically export a shapefile or feature class from Modelbuilder that has the current date in its filename?
16 Replies
KevinHibma-old
Regular Contributor III
Jeff,

I'm assuming you're on 10.
Check the toolbox I've attached. You dont need to script this.
You'll see its just a copy features and a Calc Value.
The Calc value has a python snippet to calc the date. The output of this is "outDate"

I've then used the inline variable %outDate% as the name of the featureclass.

I grabbed the snippet from:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Calculate_Field_examples/00170000004s0...
Depending on how you write out
time.strftime("%d/%m/%Y")

(d/MM/yyyy etc etc), you can get different dates. Also remember the featureclass name cant start with a number.
RyanStillman
New Contributor III

I was googling how to do something similar and this little discussion eventually lead me to my solution.

It can be done by placing a Calculate Value Tool in your model and setting its expression to:

time.strftime("%Y%m%d")

The output value will be a date or time in the format you specify between the "s. This link has a nice table with helpful explanations about the different date and time formatting options and possible associated limitations or restrictions: 8.1. datetime - Basic date and time types - Python v2.7.8 documentation‌.

My example has the particular format(%Y%m%d) such that if the model were run on June 8, 2001, the output value from the tool would be the string '20010608'.

You can then rename the output value(green bubble) to ANYTHING and then elsewhere in your model you can reference that value by inserting %ANYTHING% into filenames or location dialogues throughout the rest of the model, just like other in-line variables.

I hope this helps you out, 3½ years late, or more likely, somebody else with the same question.

SaraBarnes
New Contributor

Thanks for posting Ryan Stillman‌ - your late reply helped me figure this out!

Reply
0 Kudos
JulieScott-Ashe1
New Contributor II

Thank you for posting this - it worked perfectly.

Reply
0 Kudos
JasonHrubizna
New Contributor III

Helped me too, thanks for posting!

Reply
0 Kudos
curtvprice
MVP Esteemed Contributor

The Calculate Value tool really makes Model Builder​ a lot more useful -- and a little Python knowledge goes a long way with that thing!

Reply
0 Kudos
NedCake1
New Contributor III

Works great! Thanks for the post.

Reply
0 Kudos
DianaHutt
New Contributor

Thank you!! 

Reply
0 Kudos
AurelijaViluckyte
New Contributor

Worked for me also, thanks mate!

Reply
0 Kudos