1

I would like to use Windows task scheduler to automatically (monthly) create a new file geodatabase and then export feature classes from SDE into the new file geodatabase. Im using this code, however I get an exucute error that states that the geodatabase already exists.

# Name: CreateGDB.py
# Description: Create a file GDB
# Import system modules
import arcpy
import datetime

out_folder_path = "w:/GISDATA/Database_Backups"
nowstart = datetime.datetime.now()
YearMonthDay = nowstart.strftime("%Y_%m_%d")
out_name = "TEST_WSdata_" + YearMonthDay + ".gdb"

# Execute CreateFileGDB
arcpy.CreateFileGDB_management(out_folder_path, out_name)

arcpy.env.workspace = 'C:\Users\cbgibson\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\TEST_WSdata.sde'
# Local variables:
waterFC = ['WATERLINES', 'GRAVITY']

for fc in waterFC:
# Process: Feature Class to Geodatabase (multiple)
    arcpy.FeatureClassToGeodatabase_conversion(fc, out_name)
  • Hmmm, I actually dont want the file geodatabase to overwrite. I want to create a new file geodatabase every month. The name would include the YearMonthDay to make it unique. It almost seems like once my code creates the file geodatabase, the rest of the code tried to create it again?? Not sure. Im no expert in ArcPy. – Chad Mar 12 '15 at 19:57
  • If you have figured out your question, please create and answer, put the remedied code fix in there, and accept your answer. – artwork21 Mar 13 '15 at 15:12
2

I worked through the problem and found a solution. I also revised the code to copy ALL feature classes in the WATER dataset, instead of listing each one individually.

# Name: BackupGDB.py
# Description: Create a file GDB

# Import system modules
import arcpy
from arcpy import env # WAS MISSING IN ORIGINAL POST
import datetime

out_folder_path = "w:/GISDATA/Database_Backups"
nowstart = datetime.datetime.now()
YearMonthDay = nowstart.strftime("%Y_%m_%d")
out_name = "TEST_WSdata_" + YearMonthDay + ".gdb"

# Execute CreateFileGDB
arcpy.CreateFileGDB_management(out_folder_path, out_name)


# Set environment settings
env.workspace = "C:\Users\cbgibson\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\TEST_WSdata.sde"

# Set local variables
waterFC = arcpy.ListFeatureClasses("", "", "WATER") #CHANGED TO INCLUDE ALL FEATURE CLASSES IN THE WATER DATASET
outLocation = "w:/GISDATA/Database_Backups/" +"TEST_WSdata_" + YearMonthDay + ".gdb" 

# Execute TableToGeodatabase
for fc in waterFC:
    arcpy.FeatureClassToGeodatabase_conversion(fc, outLocation)
  • 1
    It is appropriate to answer your own question Chad if you've found the answer in the meantime unassisted by an existing answer. Though please show / highlight what the actual difference is so that future readers don't have to wade through many lines of code to see the difference. – Michael Stimson Mar 12 '15 at 23:21
0

Have a look at the overwriteOutput property.

Also check out this post.

  • 3
    This could be considered a link only answer, which is generally discouraged. It's best to at least summarize or describe the information you are linking to within your answer, as if the links go dead in the future the answer becomes useless. – Chris W Mar 12 '15 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.