Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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. Thanks!

share|improve this question

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')
share|improve this answer
Thanks, that's perfect! – tncgis Mar 11 '11 at 17:39
@tncgis, if this answers your question, please use the checkmark to mark it so. Thanks! – matt wilkie Mar 11 '11 at 18:40
Sorry to dig out and old post, but can someone explain where the layer name comes from in the calculate field formula? I don't understand the '"'+fc+'"' bit. Thanks – TreeMan Mar 11 '15 at 14:34

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.

share|improve this answer
Thanks Matt, that was a helpful start. – tncgis Mar 11 '11 at 17:40

GeoWizards is faster than the ArcGIS Append tool (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)

share|improve this answer

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(  
    ...     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

Example within MXD

share|improve this answer

Your Answer


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.