Rename Feature Classes in a File Geodatabse

11520
12
Jump to solution
01-11-2017 02:49 AM
alfrednjeru
New Contributor II

I have more than 40 feature classes in a file geodatabase. How do I rename all of them at once by replacing the prefix map212r1 with map208r1.

List Feature Classes

1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

To elaborate a little. For instance if the script is based on the one written by Joshua Bixby it would look like this:

#-------------------------------------------------------------------------------
# Name:        rename_fcs.py
# Purpose:     rename featureclasses in
#
# Author:      Joshua Bixby, Xander Bakker
#
# Created:     12/01/2017
#-------------------------------------------------------------------------------

def main():
    import arcpy
    import os

    # get parameters from tool
    gdb = arcpy.GetParameterAsText(0)
    search_for = arcpy.GetParameterAsText(1)
    replace_with = arcpy.GetParameterAsText(2)

    # search and replace
    walk = arcpy.da.Walk(gdb, datatype="FeatureClass")
    for root, dirs, files in walk:
        rename = (name for name in files if search_for in name)
        for fc in rename:
            arcpy.AddMessage("Replacing '{0}' by '{1}'".format(fc, fc.replace(search_for, replace_with)))
            arcpy.Rename_management(os.path.join(root, fc),
                                    os.path.join(root, fc.replace(search_for, replace_with)))


if __name__ == '__main__':
    main()

 

The tool would have 3 parameters:

And executing the tool would be like this:

The user will be provided information on the progress:

View solution in original post

12 Replies
XanderBakker
Esri Esteemed Contributor

In addition to the explanation provided by Asrujit SenGupta , your python script would look something like this:

import arcpy
import os

ws = r'D:\myFolder\SubFolder\myGeodatabase.gdb'  # specify your workspace here
arcpy.env.workspace = ws

datasets = arcpy.ListDatasets("*")
datasets.append('')
for dataset in datasets:
    fc_names = arcpy.ListFeatureClasses("map212r1*", "", dataset)
    for fc_name in fc_names:
        out_name = fc_name.replace("map212r1", "map208r1")
        fc_in = os.path.join(ws, dataset, fc_name)
        fc_out = os.path.join(ws, dataset, out_name)
        arcpy.Rename_management(fc_in, fc_out)

Situation before running script:

After running script:

JoshuaBixby
MVP Esteemed Contributor

In addition to Xander Bakker‌'s code, it can also be done using ArcPy Walk, which is a newer function and behaves similarly to Python's native walk function:

import arcpy
import os

gdb = # path to geodatabase

walk = arcpy.da.Walk(gdb, datatype="FeatureClass")
for root, dirs, files in walk:
    rename = (name for name in files if "map212r1" in name)
    for fc in rename:
        arcpy.Rename_management(os.path.join(root, fc),
                                os.path.join(root, fc.replace("map212r1", "map208r1")))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
alfrednjeru
New Contributor II

Thanks so much Xander Bakker and Joshua Bixby.

I have tried to run the two scripts, each separately, from Python 2.7.10 Shell.

I launched the python shell from Start Button, \ProgramFiles\All Programs\ArcGIS\Python 2.7\IDLE (Python GUI).

But nothing happens. no error message. And the feature classes are not renamed.

Am I missing something? Should I add the script to an Tool in ArcToolBox or what/ Please help.

0 Kudos
alfrednjeru
New Contributor II

I have run the script from Xander Bakker from Python Command Line and it has worked, but it has taken almost 1 minute.

But the Python Command line is not very user friendly. Any other alternative?

0 Kudos
XanderBakker
Esri Esteemed Contributor

The script only does what it is supposed to do, rename a set of featureclasses. There are no messages to show what it is doing. A very simple alternative could be to include include some print statements, but a better option would be to create a tool with a simple interface that let's you select the workspace, provide the search string and the replace string and that shows the user what it is doing. This alternative is relatively simple too.

XanderBakker
Esri Esteemed Contributor

To elaborate a little. For instance if the script is based on the one written by Joshua Bixby it would look like this:

#-------------------------------------------------------------------------------
# Name:        rename_fcs.py
# Purpose:     rename featureclasses in
#
# Author:      Joshua Bixby, Xander Bakker
#
# Created:     12/01/2017
#-------------------------------------------------------------------------------

def main():
    import arcpy
    import os

    # get parameters from tool
    gdb = arcpy.GetParameterAsText(0)
    search_for = arcpy.GetParameterAsText(1)
    replace_with = arcpy.GetParameterAsText(2)

    # search and replace
    walk = arcpy.da.Walk(gdb, datatype="FeatureClass")
    for root, dirs, files in walk:
        rename = (name for name in files if search_for in name)
        for fc in rename:
            arcpy.AddMessage("Replacing '{0}' by '{1}'".format(fc, fc.replace(search_for, replace_with)))
            arcpy.Rename_management(os.path.join(root, fc),
                                    os.path.join(root, fc.replace(search_for, replace_with)))


if __name__ == '__main__':
    main()

 

The tool would have 3 parameters:

And executing the tool would be like this:

The user will be provided information on the progress:

JoshuaBixby
MVP Esteemed Contributor

Nice, if I could give a double helpful, I would.

0 Kudos