[GIS] Exporting ArcSDE feature classes into file geodatabase with ArcPy

arcpyexportfeature-class

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)

Best Answer

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)
Related Question