Renaming multiple Featureclasses

6549
9
Jump to solution
03-29-2013 10:42 AM
RodC
by
New Contributor
Hello,

I need to create a python script that would rename all the feature classes within multiple featuredatasets in a gdb (the gdb is in ArcSDE if that makes any difference). The feature classes are in a set format and I need to change the prefix and part of the suffix.

For example: 

from "road_1234567ns" to "RD_1234567_NS12_13"

where 1234567 is the string that I need to keep (and is unique to each feature class) and prefix of "Road" is a constant that I need to replace with "RD_". Suffix is constant as well, "ns" replaced with "_NS12_13"

The renamed classes will be separated by an underscore

I am just starting out with python and eager to learn more, if anyone has any helpful insights, that would be greatly appreciated. I am using ArcGIS 10.1

Thank you!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
You could do something like below:

lstDatasets = arcpy.ListDatasets("*") for dataset in lstDatasets:     lstFCs = arcpy.ListFeatureClasses("road_*", "", dataset)     for fc in lstFCs:         if "NS" in fc:              oldName = str(fc)              newName = oldName.replace("road", "RD")              newName = newName.replace("NS", "_NS12_13")              arcpy.Rename_management(fc, newName)

View solution in original post

9 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Rodrigo,

Here is an example:

import arcpy
from arcpy import env
env.workspace = r"C:\temp\python\test.gdb"

lstDatasets = arcpy.ListDatasets("*")
for dataset in lstDatasets:
    lstFCs = arcpy.ListFeatureClasses("road_*", "", dataset)
    for fc in lstFCs:
        oldName = str(fc)
        newName = oldName.replace("road", "RD")
        newName = newName.replace("ns", "_NS12_13")
        arcpy.Rename_management(fc, newName)
danashney
New Contributor III

When I run this it only replaces the text for the last "newName" line, so in your example it would only replace the "ns" with "_NS12_13" and ignore the line that replaces "road". Any idea why that might be?

0 Kudos
MatthewBeal
Occasional Contributor III

I know this is many years after the initial post, but holy cow, thank you so much for this. Using this, I was able to automate renaming 500+ feature classes. 

0 Kudos
RodC
by
New Contributor
Hi Jake,

this worked great, thanks!

I also wanted to see if the just "ns" portion could also search featureclasses that are written in capital letters such as "NS"?
Is there a way to make this case- insensitive?

thanks again!
0 Kudos
JakeSkinner
Esri Esteemed Contributor
You could do something like below:

lstDatasets = arcpy.ListDatasets("*") for dataset in lstDatasets:     lstFCs = arcpy.ListFeatureClasses("road_*", "", dataset)     for fc in lstFCs:         if "NS" in fc:              oldName = str(fc)              newName = oldName.replace("road", "RD")              newName = newName.replace("NS", "_NS12_13")              arcpy.Rename_management(fc, newName)
RodC
by
New Contributor
Hi Jake,

this worked great, thanks!

I also wanted to see if the just "ns" portion could also search featureclasses that are written in capital letters such as "NS"?
Is there a way to make this case- insensitive?

thanks again!


Disregard this question.

I got it to work! just added another newName line

thanks!
0 Kudos
ChrisGraves1
New Contributor II

Hey Guys,

Thanks for the information. I'm looking to do the opposite where I would like to create a suffix on many different feature class names like '_1405' for example.

Thanks,

Chris

0 Kudos
DarrenWiens2
MVP Honored Contributor

Chris, you would concatenate the original featureclass name with the new suffix.

newName = str(fc) + '_1045'
0 Kudos
AREGIS
by
New Contributor

Any help on how to replace the last 3 characters across multiple feature class names? 

0 Kudos