Solved! Go to Solution.
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)
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)
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?
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.
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)
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!
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
Chris, you would concatenate the original featureclass name with the new suffix.
newName = str(fc) + '_1045'
Any help on how to replace the last 3 characters across multiple feature class names?