AnsweredAssumed Answered

Need to sort MDBs based on how many featureclasses they contain

Question asked by Sycosys on Dec 6, 2012
Latest reply on Dec 6, 2012 by Sycosys
Hello.

I have a script that takes any number of MDBs and mashes them together into a Combined MDB.

What i am trying to accomplish is to have it run in descending order from Most -> Least number of Featureclasses.

Currently it reads the MDBs in whatever order they are kept by the OS, and this can cause some issues.

Code if you need it:
import os import arcpy from arcpy import env  #Variables Block #Change The first two as needed to fit needs #Typical setup should only require running the  mxdName = "SetupRUN.mxd" #Modify if naming convention bugs you, otherwise this will be the location of the merged databases outMdb  = "Combined.mdb" #System Variables, do not modify mdbList  = [] sizeList = [] fcList   = [] sizeDB   = [] mxd  = arcpy.mapping.MapDocument("CURRENT") mxdPath  = mxd.filePath workSpace = mxdPath.replace("\\"+ mxdName , "") env.workspace = workSpace dirlist=os.listdir(workSpace)  #finds local personal geodatabases and adds them to the processing queue  for item in dirlist:     if item.lower().endswith("mdb"):         print item         mdbList.append(item) print "" print "Number of databases to be combined = " + str(len(mdbList)) print ""  #Generates Output Database and creates a list of database contents print  "Creating destination Database at: " + outMdb arcpy.CreatePersonalGDB_management(workSpace, outMdb) destSpace  = mxdPath.replace(mxdName, outMdb) env.workspace = destSpace destList = arcpy.ListFeatureClasses()  #Sorter Loop for mdb in mdbList:  tempSpace = mxdPath.replace(mxdName, mdb)  env.workspace = tempSpace  fcList = arcpy.ListFeatureClasses()  size = len(fcList)  sizeDB.append(size)  #Main loop, For each mdb in the source directory append or copy data Output Database for mdb in mdbList:  env.workspace = destSpace  destList = arcpy.ListFeatureClasses()  tempSpace = mxdPath.replace(mxdName, mdb)  env.workspace = tempSpace  fcList = arcpy.ListFeatureClasses()    print ""  print "Destination MDB - " +str(destSpace)  print "Source MDB        - " + str(tempSpace)  print ""  print fcList  print destList  if max(sizeDB) == len(fcList):   if len(destList) == 0:    print "Begin data transfer"    print ""    for fc in fcList:     arcpy.Copy_management(fc, destSpace + "\\"+fc)     print "copied " + str(fc) + " to " + str(destSpace)      elif len(destList) > 0:    print "Appending database - " +tempSpace    print ""    for fc in fcList:     for dest in destList:      if fc == dest:        arcpy.Append_management([fc], destSpace + "\\" + fc, "NO_TEST" , "" , "")       print "Feature classes Successfully appended:  " + fc + " to destination: " + dest     elif max(sizeDB) != len(fcList):   print "Appending database - " +tempSpace   print ""   for fc in fcList:    for dest in destList:     if fc == dest:       arcpy.Append_management([fc], destSpace + "\\" + fc, "NO_TEST" , "" , "")      print "Feature classes Successfully appended:  " + fc + " to destination: " + dest       for df in arcpy.mapping.ListDataFrames(mxd):   for lyr in arcpy.mapping.ListLayers(mxd,"",df):    arcpy.mapping.RemoveLayer(df, lyr)  print ""   print "Merge complete"

Outcomes