ListBrokenDataSources not working

676
8
09-06-2012 02:13 AM
LucaMoiana
New Contributor
In my SHP to GDB toolbox, I create a list of layers in MXD, then I check for those not broken I move them. Despite using ListBrokenDataSources I have errors like this:

Runtime error <type 'exceptions.WindowsError'>: [Error 3] Impossibile trovare il percorso specificato: u'J:\\20120208_Backup_disco_E\\Archivio\\ATLArete\\Shape Atlarete 1-1-2008\\miste.shp'

I'm desperate at this point, any help????
Following my code:

# 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)
Tags (2)
0 Kudos
8 Replies
JeffBarrette
Esri Regular Contributor
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
0 Kudos
LucaMoiana
New Contributor
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


Hi Jeff,

Thanks a lot for your reply.
Layers can't be in the gdb dataset cause, in the first part of the script a I create a new feature dataset.
0 Kudos
JeffBarrette
Esri Regular Contributor
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
0 Kudos
LucaMoiana
New Contributor
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...
0 Kudos
LucaMoiana
New Contributor
New feature dataset in an existing GDB.

Still struggling...




Apologies,
I just found out that I forgot this part in my script:

#################
# REPLACE DATASOURCE
new_path = 'I:\\20120208_Backup_disco_E\\'
mxd.findAndReplaceWorkspacePaths('E:\\', new_path)
arcpy.RefreshTOC()
Sorry again
0 Kudos
ChrisPedrezuela
Occasional Contributor III
Hi guys,

I am using version 10 and I just noticed that this code (ListBrokenDataSources only) works properly only in python window.

When I run the .py file outside ArcGIS, it lists "all" the layers in the mxd and not the the broken one only for several runs, and after a while, it reports the correct broken layer.

Could any one of you kindly let me know why I am experiencing this issue?

Thanks,
Chris
0 Kudos
JeffBarrette
Esri Regular Contributor
Chris,

I could not reproduce on 10.0 SP5.  What version/service pack are you running?  Could you provide a script and a description of the type of datasets in your MXD?

Jeff
0 Kudos
ChrisPedrezuela
Occasional Contributor III
Thanks Jeff. I just used the sample code provided in the help docs for listbrokendatasources. I currently do not have a service pack installed so ill try to get one or maybe test on a pc with 10.1 and see what happens.

Chris
0 Kudos