I have over 60 shapefiles, all in their own folders. I'm looking to add a field titled "Name" and then populate it with the name of the shapefile. I want to be able to do this so that I can then merge the files together while retaining a unique identifier for each. I can do this by hand of course, but would like to script it. I don't know Python and I'm familiar though not very experienced with ModelBuilder. I'm using ArcGIS 10.
This will do it
import arcpy arcpy.env.workspace = r'c:\temp\x' fcs = arcpy.ListFeatureClasses() for fc in fcs: arcpy.AddField_management(fc, 'shpname','text') arcpy.CalculateField_management(fc, 'shpname', '"'+fc+'"') arcpy.Merge_management(fcs, 'out.shp')
For python the pieces needed are: os.path.walk to recurse through the directories, arcpy.ListFeatureClasses in each to generate lists of shapes to operate on, from there you can use the add field tool to add and populate a 'Name' field.
I'll let someone else address a modeller or toolbox approach, with one parting shot: most people don't realise all the tools have a batch mode. Right click on the tool, select batch, then drag'n'drop inputs from Arccatalog. Sometimes a lot faster for the human than building a model or script.
GeoWizards is faster than the ArcGIS Append tool
http://www.ian-ko.com/ET_GeoWizards/UserGuide/mergeLayers.htm (preserves file name locations)
If purchased you will have access to the tools and the scripts to execute the function you want. (the free version does not allow this scripting function)
I prefer to load the shapefiles all into one MXD then run the code through python. Below is my little spin on it. No adjustments needed except adding the merge function if that is your desire, but it's easily done after the fact via the toolbox.
Copy and paste into Python within the MXD then hit enter after each line. This helps with the auto-indention which can cause frustration.
>>> mxd = arcpy.mapping.MapDocument("Current") >>> layerlist = arcpy.mapping.ListLayers(mxd) >>> x = 0 >>> while x < len(layerlist): ... layer = layerlist[x] ... layername = str(layer.name) ... expression = "'" + layername + "'" ... arcpy.AddField_management(layer, "identify", "TEXT") ... arcpy.CalculateField_management (layer, "identify", expression, "PYTHON_9.3", "#") ... x = x + 1 ... At this point you can watch it run in ArcMap