Batch Renaming Relationship Classes using ArcPy

by FGoodison   Last Updated October 19, 2019 01:22 AM - source

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.



Answers 2


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.

Adam
Adam
November 18, 2014 19:36 PM

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)
Aaron
Aaron
January 03, 2015 15:34 PM

Related Questions



CopyDatasets will not overwrite some datasets

Updated June 12, 2015 23:09 PM


Batch Rename Relationship Classes

Updated December 21, 2017 00:22 AM