Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I need to create a python script (or model) that would first rename all the feature datasets in a gdb. The feature dataset is in a set format and I need to change the prefix of the dataset.

For example: from "H2611111:xxx" to "gen26_xxx" - where xxx is the string that I need to keep and prefix of "H2611111" is a constant that I need to replace with "gen26_".

Once this is completed, I would then need to rename each feature class inside each renamed dataset. The feature classes needs to be prefixed with the newly renamed dataset separated by an underscore.

For example:

in dataset gen26_xxx: from "test1" to "gen26_xxx_test1" from "test2" to "gen26_xxx_test2"

in dataset gen26_zzz: from "new1" to "gen26_zzz_new1"

I am new to python (and model building), if anyone has any helpful insights, that would be greatly appreciated. I am using ArcGIS 10 and 9.3.1

Thank you

share|improve this question
1  
Why exactly do you want to script this? Are there too many to do it manually? Also the : in your first example is not a valid character IIRC. –  blah238 Nov 26 '12 at 22:10
add comment

1 Answer 1

A good solution is a combination of ListDatasets() and Rename().

The basic solution is:

  1. Set your workspace
  2. Use arcpy.ListDatasets() to get a list of all datasets.
  3. Loop through the datasets list
  4. For each dataset in the list, call arcpy.Rename_management(oldName, newName)
  5. If the names are as you stated in your post, then the newName is 'gen26_' + oldName.split(':')[1]

To do the same now for each feature class within the datasets.

  1. Set your workspace
  2. Use arcpy.ListDatasets() to get a list of all the datasets (since you just renamed them)
  3. For each dataset in the list, change the workspace to be that dataset
  4. Now call arcpy.ListFeatureClasses() to get a list of the feature classes
  5. Use arcpy.Rename_management() like above with the new name

Note you can easily combine the above steps if you list datasets, list the feature classes in the workspace, rename all the feature classes, and now you can rename the dataset as well. For more on string manipulation, read here

share|improve this answer
1  
Good solution @Michalis. A slight modification to that process might be to store the new dataset names in a list as they are being renamed. That way, you don't need to use arcpy.ListDatasets() a second time. If there are a lot of datasets, this may save some time. –  Fezter Nov 26 '12 at 22:34
    
@Michalis, Thank you very much, I just ran the code for the change dataset and it worked. Now to work it so it will work on the feature classes. JeffC –  JeffC Nov 26 '12 at 23:01
    
@Michalis, Once again, thank you very much. The feature classes are now being renamed with the dataset prefix. This is such a time saver. –  JeffC Nov 26 '12 at 23:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.