I have a couple hundred file geodatabases (GDBs) each containing several feature classes.

Is there a way for me to move the GDBs into an mxd such that each GDB is imported as a group layer containing the feature classes as sub-layers?

I'm specifically looking for an arcpy script to do this since there are so many of the GDBs.

  • Are all the GDBs in the same folders, or in different folders? If they are in different folders, how are those folders arranged? Commented Oct 30, 2015 at 19:56
  • They are all in the same folder, the hierarchy is simply GdbFolder ->GDBs -> feature class
    – purelyp93
    Commented Oct 30, 2015 at 20:00
  • The code will be similar to gis.stackexchange.com/q/115046/115 although there layer groups come from feature datasets rather than separate file geodatabases.
    – PolyGeo
    Commented Oct 30, 2015 at 20:01

1 Answer 1


This code is totally untested, but hopefully it works. The code requires three inputs. The first is the full path to your folder containing the gdbs. The second is the saved map document file you wish to add the layers to. The third is an empty group layer file (.lyr). The code basically an iteration through your geodatabases, creating a new group layer for each, and then iterating and adding each feature class as a layer to the group layer.

#Full path to folder containing GDBs
inFolder = r"C:\Test\folder"

#Map document to add layers to
mapDoc = r"C:\Test\mapdoc.mxd"

#Group layer .lyr file
grpLyr = r"C:\Test\grouplayer.lyr"

#----End locals

#import modules
print "importing"
import arcpy
import os

#create mxd objects
print "creating map objects"
mxd = arcpy.mapping.MapDocument (mapDoc)
df = arcpy.mapping.ListDataFrames (mxd)[0]

#set arcpy env workspace to input folder
print "setting workspace"
arcpy.env.workspace = inFolder

#list geodatabases
print "listing geodatabases"
gdbs = arcpy.ListWorkspaces (workspace_type = "FileGDB")

#iterate geodatabases
print "iterating", len (gdbs), "file geodatabases"

for gdb in gdbs:

    print "exploring", gdb

    #create group layer object
    groupLayer = arcpy.mapping.Layer (grpLyr)

    #rename layer to gdb name
    groupLayer.name = gdb

    #add group layer to data frame
    arcpy.mapping.AddLayer (df, groupLayer)

    #change workspace to gdb
    gdbFullPath = os.path.join (inFolder, gdb)
    arcpy.env.workspace = gdbFullPath

    #list feature classes in gdb
    fcs = arcpy.ListFeatureClasses ()

    #iterate fcs
    print "adding", len (fcs), "layers"
    for fc in fcs:

        #create feature class layer object
        lyr = arcpy.mapping.Layer (fc)

        #add layer to group layer
        arcpy.AddLayerToGroup (df, groupLayer, lyr)

print "saving map file"
mxd.save ()

del mxd
del df
print "done"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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