Rename All Feature Classes in Multiple .sde DB's

773
6
01-22-2021 06:57 AM
fgillen1
New Contributor

Hi all,

I have a folder with around 50 .sde DB's. Within these .sde DB's there are around 450 feature classes which have been named to a certain standard. This standard has changed and I now need to update the feature class names. 

The good part about the renaming is that I have to remove characters after the 3rd character up until the an '_'. 

If anyone has any code available to help that would be great.

Thanks,

Finbar

 

0 Kudos
6 Replies
DanPatterson
MVP Esteemed Contributor

like this?

n = "abcdef_ghij"

n.replace(n[3:n.find("_")+1],"")

'abcghij'

... sort of retired...
fgillen1
New Contributor

Exactly like that... have to put this through some kind of for loop now in order to rename all feature classes in all .sde's in the folder... any ideas?

0 Kudos
DanPatterson
MVP Esteemed Contributor

probably a

ListWorkspaces—ArcGIS Pro | Documentation

with a judicious use of 

ListFeatureClasses—ArcGIS Pro | Documentation 

when you are in a workspace

with os.walk or

Walk—ArcGIS Pro | Documentation

controlling how you cycle through the things in the folder


... sort of retired...
DavidPike
MVP Frequent Contributor

not tested or anything, also please don't do this straight into a PROD environment.  Sure there's more efficient ways but will probably do the job.

I'd imagine you'd need to stop your services/locks etc also.

throw in Dan's replace statement for new_fc

 

 

import arcpy
import os

#folder containing all the .sde connection
#files you want to rename FCs in
sde_folder = r'D:\Downloads\tester'

#list all items in directory and append
#to sde_path list where .sde is in the
#filename
sde_paths = []
dir_list = os.listdir(sde_folder)
for item in dir_list:
    if '.sde' in item:
        sde_path = os.path.join(sde_folder, item)
        sde_paths.append(sde_path)


#iterate over each .sde connection (EGDB)
        #and rename all your stuff
for sde_path in sde_paths:

    arcpy.env.workspace = sde_path
    featureclasses = arcpy.ListFeatureClasses()

    for fc_old in featureclasses:
        fc_new = #try what Dan suggests
        arcpy.Rename_management(fc_old, fc_new)

 

BlakeTerhune
MVP Regular Contributor

Keep in mind that you'll probably need to connect as the owner of the table to rename it. Depending on how your database is organized, you may need to work out a way to reconnect as the appropriate user each time you encounter a new schema. I did something similar here that you can use for ideas.

DanPatterson
MVP Esteemed Contributor

And dont' forget... with all the best plans laid, there will be some featureclasses that don't conform to your naming plan 😉


... sort of retired...