Select to view content in your preferred language

Create a folder in modelbuilder with today's date

662
6
01-25-2024 08:14 AM
KevinDicks
New Contributor

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!

0 Kudos
6 Replies
EdMorris
Esri Contributor

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 NameLabel for the script tool.

5: On the Parameters tab you need to create 2 parameters here.

The first parameter is the geodatabase pathway.

  1. Provide a label. This is what the user will see as the title of the parameter on the dialog.
  2. Select workspace as the datatype. You should set a filter, choosing workspace and then tick File System

The second parameter is the geodatabase name.

  1. Provide a label. This is what the user will see as the title of the parameter on the dialog.
  2. Set a default value, for example Streets.gdb.

So:

EdMorris_0-1706206028735.png

 

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:

EdMorris_2-1706206839253.png

 

The script tool dialog will then display.

EdMorris_1-1706206756794.png

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

0 Kudos
DavidPike
MVP Frequent Contributor

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

Bud
by
Honored Contributor

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

FunctionDescription

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.

0 Kudos
EdMorris
Esri Contributor

Morning @KevinDicks 

I think @DavidPike suggestion is a much more elegant solution so i would suggest you go David's. Nicely done David.

0 Kudos
DavidPike
MVP Frequent Contributor

From the man who taught me my first Python with arcpy. Thanks @EdMorris 

0 Kudos
KevinDicks
New Contributor

Thanks for the suggestions everyone!  I will try these out.  

0 Kudos