I tried some of the scripts I found but none worked so far. What I am trying to do is batch merging hundreds, maybe thousands of shapefiles from different folders/sub-folders into a single shp or feature class (I read is better in case of large data). My data is stored in [Shape Files] folder, as you may see here:

  • O:\ABC\DEF\2010\project_nu\Shape Files*.shp
  • O:\ABC\DEF\2011\project_nu\Shape Files*.shp
  • O:\ABC\DEF\2012\project_nu\Shape Files*.shp
  • O:\ABC\DEF\2013\project_nu\Shape Files*.shp
  • O:\ABC\DEF\2014\project_nu\Shape Files*.shp

(where there's many project_nu in each year)

I don't have any experience in Python just in ModelBuilder.

I did a little bit of research and I understand that first I need to use the os.walk through all these folders and search for *.shp polygons (all shp's are polygon so there's no need to test this). Then apply merge to the generated list.

I tried ModelBuilder too (Iterate feature class-> Collect Values-> Merge) and got stuck in the iterative process that kept running with no results (I even tried for a few shps).

  • 3
    What software are you using? What version? (Please update the question). Technically, shapefile names which are not "8.3" formatted are non-compliant. Putting spaces in a shapefile name is a big no-no. – Vince Dec 22 '14 at 23:32
  • Simply use windows explorer, search for *.shp and add entire list (drag and drop) to Merge tool interface. Assuming you are using ArcGIS – FelixIP Dec 23 '14 at 1:32
  • I am using ArcGIS 10.1. I need a script cause I can't drag and drop thousands of shps spread in a lot of folders each month. It would be a waste of time. – Luiza Dec 23 '14 at 16:10

you can create a list of many shapefiles using the glob module or os.walk, then you can use this list directly in the merge tool.

import arcpy, glob

yourlist = glob.glob(r"O:\ABC\DEF\*\project_nu\*.shp)
arcpy.Merge_management(yourlist, r"C:\output.shp")

Assuming that you are using ArcGIS 10.1 SP1 (or later) for Desktop I think you are better to look past os.walk and glob because arcpy.da.Walk is more spatial data aware and can:

Generate data names in a directory/database structure by walking the tree top-down or bottom-up. Each directory/workspace yields a tuple of three: directory path, directory names, and file names.


You can do this with os.walk and fnmatch with very little code. Try this:

import arcpy
import fnmatch, os

root = "whatever top directory here"
outdir = "wherever to output"
search = "*.shp"
outname = "whatever output name"

shpmerge = [] #This is simply an empty list to collect shapes.
for root, dir, files in os.walk(root):
    for filename in fnmatch.filter(files, search):
        shpmerge.append(os.path.join(root, filename))

if shpmerge:
    outshp = os.path.join(outdir, os.path.basename(outname[0][:-4]) + "_Merge.shp")
    arcpy.Merge_management(shpmerge, outshp)

This is untested, but just some quick code - should work.

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