Hi Community.
I created a model to package up some data to send off to another office. One part of my model is a new folder for the new file geodatabase. Is there a way to automatically name the folder to today's date?
Thanks!
Hello!
I think one way you can do this is via a Python script tool.
I would carry out the following:
1: Copy the code below into a Python script:
import arcpy
from datetime import datetime# Module for calculating dates & times
path = arcpy.GetParameterAsText(0) # Receive data from Script tool dialog
gdbName = arcpy.GetParameterAsText(1) # Receive data from script tool dialog
# Calculate the date (you can amend this to add the time also..)
curDate = datetime.now()
year = curDate.year
month = curDate.month
day = curDate.day
gdbNameDate = f"{year}_{month}_{day}_{gdbName}"
#Call the Create File Geodatabase geoprocessing tool
arcpy.arcpy.management.CreateFileGDB(path, gdbNameDate)
2: The lines of code - arcpy.GetParameterAsText() allow the script to receive data from outside of the script. You will pass the geodatabase pathway and its name into the script. GetParameterAsText(0) receives the pathway for the geodatabase while GetParameterAsText(1) receives the chosen name of the geodatabase.
3: Create a Python Script tool in, for example, your project's default toolbox. (Right click the toolbox > New > Script)
This will display the Python Script tool properties dialog. There are 4 tabs down the left hand side.
4: On the General tab, provide a Name & Label for the script tool.
5: On the Parameters tab you need to create 2 parameters here.
The first parameter is the geodatabase pathway.
The second parameter is the geodatabase name.
So:
The ordering of these parameters on the Tool Properties dialog is important as the order on the dialog determines the order in which the values are passed into the script via arcpy.GetParameterAsText()
6: Click the Execution tab and for Script File browse to the location of the python script!
7: Once your are finished double click the Python Script tool:
The script tool dialog will then display.
8: Fill in the dialog parameters and press Run.
That should be everything you need to know... and will create a new file geodatabase with the current date as part of its name...
Have fun ed
You can add a Calculate Field model builder tool which has a Python script to calculate the date and output as a string. You'd then use the output (I think defaults to 'Value') in an inline variable substitution for the output filename for whatever tool.
https://pro.arcgis.com/en/pro-app/latest/tool-reference/modelbuilder-toolbox/calculate-value.htm
Python script (not in codeblock, just in the expression box. Data Type=String.
datetime.datetime.now().strftime("%Y_%m_%d")
That should then have an output called 'Value' (you can rename this of-course)
Then when you write the output filename in another tool:
%Value%_myFilename.extension
goes to -> 2024_01_25_myFilename.extension
This isn’t my area of expertise, but for anyone like me who’s looking for more info, just Google "arcgis" "inline variable":
https://www.google.ca/search?q=%22arcgis%22+%22Inline+variable%22
A somewhat related idea: Inline variable for populating date datatype field
I suppose it might also be possible to get the system date using SQL in a database view. Or maybe even editor tracking.
SQL for reporting and analysis on file geodatabases
Date Functions
Function | Description |
CURRENT_DATE | Returns the current date. |
EXTRACT (extract_fieldFROM extract_source) | Returns the extract_field portion of theextract_source. The extract_source argument is a date-time expression. The extract_field argument can be one of the following keywords: YEAR, MONTH, DAY, HOUR, MINUTE, or SECOND. |
CURRENT TIME | Returns the current time. |
CURRENT_TIMESTAMP | Returns the current time and date. |
Morning @KevinDicks
I think @DavidPike suggestion is a much more elegant solution so i would suggest you go David's. Nicely done David.
From the man who taught me my first Python with arcpy. Thanks @EdMorris
Thanks for the suggestions everyone! I will try these out.