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.
To elaborate a little. For instance if the script is based on the one written by Joshua Bixby it would look like this:
<SPAN class="comment token">#-------------------------------------------------------------------------------</SPAN> <SPAN class="comment token"># Name: rename_fcs.py</SPAN> <SPAN class="comment token"># Purpose: rename featureclasses in</SPAN> <SPAN class="comment token">#</SPAN> <SPAN class="comment token"># Author: Joshua Bixby, Xander Bakker</SPAN> <SPAN class="comment token">#</SPAN> <SPAN class="comment token"># Created: 12/01/2017</SPAN> <SPAN class="comment token">#-------------------------------------------------------------------------------</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">main</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">import</SPAN> arcpy <SPAN class="keyword token">import</SPAN> os <SPAN class="comment token"># get parameters from tool</SPAN> gdb <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN> search_for <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN> replace_with <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># search and replace</SPAN> walk <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>Walk<SPAN class="punctuation token">(</SPAN>gdb<SPAN class="punctuation token">,</SPAN> datatype<SPAN class="operator token">=</SPAN><SPAN class="string token">"FeatureClass"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> root<SPAN class="punctuation token">,</SPAN> dirs<SPAN class="punctuation token">,</SPAN> files <SPAN class="keyword token">in</SPAN> walk<SPAN class="punctuation token">:</SPAN> rename <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>name <SPAN class="keyword token">for</SPAN> name <SPAN class="keyword token">in</SPAN> files <SPAN class="keyword token">if</SPAN> search_for <SPAN class="keyword token">in</SPAN> name<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> fc <SPAN class="keyword token">in</SPAN> rename<SPAN class="punctuation token">:</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Replacing '{0}' by '{1}'"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">,</SPAN> fc<SPAN class="punctuation token">.</SPAN>replace<SPAN class="punctuation token">(</SPAN>search_for<SPAN class="punctuation token">,</SPAN> replace_with<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Rename_management<SPAN class="punctuation token">(</SPAN>os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>root<SPAN class="punctuation token">,</SPAN> fc<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>root<SPAN class="punctuation token">,</SPAN> fc<SPAN class="punctuation token">.</SPAN>replace<SPAN class="punctuation token">(</SPAN>search_for<SPAN class="punctuation token">,</SPAN> replace_with<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> __name__ <SPAN class="operator token">==</SPAN> <SPAN class="string token">'__main__'</SPAN><SPAN class="punctuation token">:</SPAN> main<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
The tool would have 3 parameters:
And executing the tool would be like this:
The user will be provided information on the progress:
There are a couple options. One is to copy and paste the code into the interactive Python window in ArcMap or ArcCatalog. Another is to create a script tool where you could populate some parameters via dialog boxes at runtime. Developing a script tool may take some learning and time if you are new to Python, so in the near term copying and pasting might work best.
Regarding using IDLE, I am a bit unclear of your steps. You launch IDLE, obviously, but how are you loading the script into it? And, how are you running it? For my script, you will need to replace gdb with a full path to a file geodatabase; otherwise, it won't have anything to act on.
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.
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?
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.
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:
<SPAN class="keyword token">import</SPAN> arcpy <SPAN class="keyword token">import</SPAN> os gdb <SPAN class="operator token">=</SPAN> <SPAN class="comment token"># path to geodatabase</SPAN> walk <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>Walk<SPAN class="punctuation token">(</SPAN>gdb<SPAN class="punctuation token">,</SPAN> datatype<SPAN class="operator token">=</SPAN><SPAN class="string token">"FeatureClass"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> root<SPAN class="punctuation token">,</SPAN> dirs<SPAN class="punctuation token">,</SPAN> files <SPAN class="keyword token">in</SPAN> walk<SPAN class="punctuation token">:</SPAN> rename <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>name <SPAN class="keyword token">for</SPAN> name <SPAN class="keyword token">in</SPAN> files <SPAN class="keyword token">if</SPAN> <SPAN class="string token">"map212r1"</SPAN> <SPAN class="keyword token">in</SPAN> name<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> fc <SPAN class="keyword token">in</SPAN> rename<SPAN class="punctuation token">:</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Rename_management<SPAN class="punctuation token">(</SPAN>os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>root<SPAN class="punctuation token">,</SPAN> fc<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>root<SPAN class="punctuation token">,</SPAN> fc<SPAN class="punctuation token">.</SPAN>replace<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"map212r1"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"map208r1"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
In addition to the explanation provided by Asrujit SenGupta , your python script would look something like this:
<SPAN class="keyword token">import</SPAN> arcpy <SPAN class="keyword token">import</SPAN> os ws <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'D:\myFolder\SubFolder\myGeodatabase.gdb'</SPAN> <SPAN class="comment token"># specify your workspace here</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> ws datasets <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListDatasets<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"*"</SPAN><SPAN class="punctuation token">)</SPAN> datasets<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN><SPAN class="string token">''</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> dataset <SPAN class="keyword token">in</SPAN> datasets<SPAN class="punctuation token">:</SPAN> fc_names <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListFeatureClasses<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"map212r1*"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN> dataset<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> fc_name <SPAN class="keyword token">in</SPAN> fc_names<SPAN class="punctuation token">:</SPAN> out_name <SPAN class="operator token">=</SPAN> fc_name<SPAN class="punctuation token">.</SPAN>replace<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"map212r1"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"map208r1"</SPAN><SPAN class="punctuation token">)</SPAN> fc_in <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>ws<SPAN class="punctuation token">,</SPAN> dataset<SPAN class="punctuation token">,</SPAN> fc_name<SPAN class="punctuation token">)</SPAN> fc_out <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>ws<SPAN class="punctuation token">,</SPAN> dataset<SPAN class="punctuation token">,</SPAN> out_name<SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Rename_management<SPAN class="punctuation token">(</SPAN>fc_in<SPAN class="punctuation token">,</SPAN> fc_out<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Situation before running script:
After running script:
I will move to https://community.esri.com/community/gis/managing-data?sr=search&searchId=b5accd95-bd33-4ac7-988e-cac07ab49a77&searchIndex=1
Check the below links and see if they help:
Batch Rename Feature Classes?
Renaming multiple Featureclasses
arcpy - How can I rename multiple feature classes (each different name) using Python? - Geographic Information Systems S…
Also please move this post to a more suitable place like https://community.esri.com/groups/geodatabase?sr=search&searchId=a6df4101-e03a-4920-aa87-e2ef90062826&searchIndex=0 https://community.esri.com/community/gis/managing-data?sr=search&searchId=a00da523-bc13-4839-a5b1-06e512f71c58&searchIndex=0 https://community.esri.com/community/developers/gis-developers/python?sr=search&searchId=04359912-2040-4385-9e27-da934b08209e&searchIndex=1
Membros conectados podem postar, seguir atualizações e mais. Novo aqui? Registre uma conta gratuita.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.