0

I'm trying to rename about 100 relationship classes in a Geodatabase. All the relationship classes are named under a naming convention and a standard part of that has changed, i.e:

abc_feature1_rel

abc_feature2_rel

Renamed to:

ddd_feature1_rel

ddd_feature2_rel

I would like to create a small script to automate this if possible, rather than doing it manually.

1

This code snippet should work for File GDB relationship classes. Uses the Describe object properties (arcpy) and the Rename (Data Management) tool. If your data is in SDE then you will need to account for the fully qualified names, ex. database.schema.abc_feature1_rel

import arcpy
import os

desc = arcpy.Describe(r'C:\Users\<username>\Documents\ArcGIS\Default.gdb')

for child in desc.children:
    if child.datatype == "RelationshipClass":
        new_name = "ddd" + child.name[3:]
        arcpy.Rename_management(child.catalogpath, os.path.join(child.path, new_name))

I was initially having trouble listing the Relationship Classes in the GDB (there is not a "List RelationshipClasses" arcpy function) but found this forum post to be helpful.

1

This approach uses a list comprehension to rapidly generate the list of relationship classes. The input name is then split by "_" and reconstructed to your specifications.

import arcpy, os

workspace = r'C:\Users\OWNER\Documents\ArcGIS\Default.gdb'

# Generate the list of relationship classes
rc_list = [c.name for c in arcpy.Describe(workspace).children if c.datatype == "RelationshipClass"]

# Loop through the list and rename
for rc in rc_list:
    outname = "ddd" + "_" + rc.split("_")[1] + "_" + rc.split("_")[2]
    arcpy.Rename_management(os.path.join(workspace,rc), outname)

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.