[GIS] Batch shapefile merge, with adding and calculating field, in ArcPy

arcgis-10.0arcpy

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.

Best Answer

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')
Related Question