# Create a list of SHP in mxd list = [] df_list = arcpy.mapping.ListDataFrames (mxd) for df in df_list: for fc in arcpy.mapping.ListLayers (mxd, "", df): if (fc.supports("DATASOURCE")) and (fc.dataSource.endswith(".shp")): list.append(fc) print("List created") # Check for borken datasource for fc in list: if arcpy.mapping.ListBrokenDataSources(fc): list.remove(fc) print ("Broken data removed from list") # Move fc to gdb add_list = [] for fc in list: fc_date = time.strftime("%Y%m%d", time.gmtime(os.path.getctime(fc.dataSource))) try: dsc = arcpy.Describe(fc) out_temp = '_' + fc_date + '_' + fc.name + '_' + fc_prj out_name = arcpy.ValidateTableName(out_temp) print ('Created ' + out_name + ' to ' + gdb_full_path) except: out_temp = '_' + fc_date + '_' + fc.name + '_' + 'UNKNOWN' out_name = arcpy.ValidateTableName(out_temp) print ('Created ' + out_name + ' to ' + gdb_full_path) arcpy.FeatureClassToFeatureClass_conversion(fc, fd_path, out_name) replace_name = fd_path + '\\' + out_name fc.replaceDataSource (gdb_full_path, "FILEGDB_WORKSPACE", out_name) print ('Replaced data source ' + out_name + ' in ' + gdb_full_path) # Create a new MXD new_mxd = os.path.splitext(gdb_full_path)[0] + '\\' + 'MXD' + '\\' + time.strftime("%Y%m%d", mxd_date )+ '_' + (mxd_name) mxd.saveACopy(new_mxd) print ('Created new MXD ' + new_mxd + ' in ' + ws)
import arcpy mxd = arcpy.mapping.MapDocument(r"C:\Temp\Shapefiles.mxd")# Create a list of SHP in mxd list = [] df_list = arcpy.mapping.ListDataFrames(mxd) for df in df_list: for fc in arcpy.mapping.ListLayers(mxd, "", df): if (fc.supports("DATASOURCE")) and (fc.dataSource.endswith(".shp")): list.append(fc) print list # Check for broken datasource for fc in list: if arcpy.mapping.ListBrokenDataSources(fc): list.remove(fc) print list # Move fc to gdb ws = r"C:\Temp\Temp.gdb" for fc in list: dsc = arcpy.Describe(fc) arcpy.FeatureClassToFeatureClass_conversion(fc, ws, fc.name) fc.replaceDataSource (ws, "FILEGDB_WORKSPACE", fc.name) # Create a new MXD mxd.saveACopy(r"C:\Temp\Shapefiles2.mxd")
I tried to reproduce your issue by simplifying your code. It works. I tested on 10.1 final. I added two shapefiles to a new MXD. I renamed one of them so it would appear broken in the MXD. I created a new, empty file GDB. Finally I ran the following, simplified code:import arcpy mxd = arcpy.mapping.MapDocument(r"C:\Temp\Shapefiles.mxd")# Create a list of SHP in mxd list = [] df_list = arcpy.mapping.ListDataFrames(mxd) for df in df_list: for fc in arcpy.mapping.ListLayers(mxd, "", df): if (fc.supports("DATASOURCE")) and (fc.dataSource.endswith(".shp")): list.append(fc) print list # Check for broken datasource for fc in list: if arcpy.mapping.ListBrokenDataSources(fc): list.remove(fc) print list # Move fc to gdb ws = r"C:\Temp\Temp.gdb" for fc in list: dsc = arcpy.Describe(fc) arcpy.FeatureClassToFeatureClass_conversion(fc, ws, fc.name) fc.replaceDataSource (ws, "FILEGDB_WORKSPACE", fc.name) # Create a new MXD mxd.saveACopy(r"C:\Temp\Shapefiles2.mxd")
One possible issue is that you are running the code multiple times and the FCs may already exist in the output GDB. You need to remove those manually ahead of time or check to see if they exist and remove them with code before the FeatureClassToFeatureClass function is called.
Jeff
A new feature dataset or a new GDB? Feature classes with the same name can't exist in two different feature datasets in the same GDB. I.e., all feature classes in a GDB must have a unique name.
Jeff
New feature dataset in an existing GDB.
Still struggling...
################# # REPLACE DATASOURCE new_path = 'I:\\20120208_Backup_disco_E\\' mxd.findAndReplaceWorkspacePaths('E:\\', new_path) arcpy.RefreshTOC() Sorry again