1

I have huge amount of personal geodatabases that contain dataset and feature classes (one dataset with four feature classes and one feature classes outside of the dataset in each mdb).

I want to create a file geodatabase for each of mdb and copy the dataset and feature classes in the file geodatabases.

For creating file geodatabse i use the below code but i don't know how to copy the datasets and feature classes to the file geodatabase programmatically.

import arcpy,os
for file in os.listdir(r"X:\test\2"):
    if file.endswith(".mdb"):
        arcpy.CreateFileGDB_management(r"X:\test\2",file[:-4],"CURRENT")
3

The following script converts all of the feature datasets and feature classes of a personal geodatabase to a new file geodatabase.


import arcpy, os

# Set the in and out workspaces
inws = r'C:\temp'
outws = r'C:\temp\outws'

# List the Access Databases
arcpy.env.workspace = inws
mdbs = arcpy.ListWorkspaces(workspace_type = "Access")

count = 1
for m in mdbs:
    # Define output FGDB name
    fgdb_name = os.path.basename(m).split(".")[0] + ".gdb"

    # Create a new FGDB
    arcpy.CreateFileGDB_management(outws, fgdb_name)

    # Copy any FCs that are directly in personal geodatabase
    arcpy.env.workspace = os.path.join(inws, m)
    fcs = arcpy.ListFeatureClasses()
    for fc in fcs:
        arcpy.CopyFeatures_management(fc, os.path.join(outws, fgdb_name, fc))

    # Report on processing status
    print "%s of %s personal databases converted to FGDB" % (count, len(mdbs))
    count += 1

    # List the feature dataset
    arcpy.env.workspace = os.path.join(inws, m)
    fds = arcpy.ListDatasets()
    for f in fds:
        # Determine FDS spatial reference
        desc = arcpy.Describe(f)
        sr = desc.spatialReference

        # Copy FDS to FGDB
        arcpy.CreateFeatureDataset_management(os.path.join(outws, fgdb_name), f, spatial_reference = sr)

        # Copy the FCs to new FDS
        arcpy.env.workspace = os.path.join(inws, m, f)
        fcs = arcpy.ListFeatureClasses()
        for fc in fcs:
            arcpy.CopyFeatures_management(fc, os.path.join(outws, fgdb_name, f, fc))
  • Thanks . I have another problem . I have attachments in a polygon Feature class in each personal geodatabase. The above code does not copy attachments .Is ther any solution to copy attachments from personal to fiel geodatabase ? – BBG_GIS Aug 15 '15 at 7:30
  • What type of attachments? – Aaron Aug 15 '15 at 18:50
  • I attached CAD file using create attachment in personal geodatabase – BBG_GIS Aug 16 '15 at 9:14
3

Assuming that your code is working OK to create file geodatabases with the same names as your personal geodatabases, I think your next steps should be to:

  • use ListDatasets to produce a list of feature datasets in your Personal Geodatabase, and then
  • use Create Feature Dataset to create new ones with the same names in your File Geodatabase.
  • Thanks but i dont know how to wirte code of your solution – BBG_GIS Aug 2 '15 at 9:27
0

From the answer above, you need to replace where it says:

desc = arcpy.Describe(f)

by this:

f_path = os.path.join(inws, m, f)
desc = arcpy.Describe(f_path)

Otherwise, after copying the first encountered dataset and facing the second dataset from the list, the Describe function cannot find the dataset and it will pop-up an error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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