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.
Solved! Go to Solution.
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:
Check the below links and see if they help:
Renaming multiple Featureclasses
Also please move this post to a more suitable place like https://community.esri.com/groups/geodatabase?sr=search&searchId=a6df4101-e03a-4920-aa87-e2ef9006282... https://community.esri.com/community/gis/managing-data?sr=search&searchId=a00da523-bc13-4839-a5b1-06... https://community.esri.com/community/developers/gis-developers/python?sr=search&searchId=04359912-20...
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:
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")))
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.
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?
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.
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:
Nice, if I could give a double helpful, I would.