Batch merge multiple shapefiles - with confusing file structure

12-24-2013 08:36 AM
New Contributor
I have a series of folders that have different shapefiles in each one. The naming convention is odd. All I need to do is search through each subfolder and get the shapefiles ending with *_CadastralPolygons.shp. I figured I could output it to a holding folder for now (called "hold"). The naming convention doesnt matter either, I just need to get this working in my "testing" folder then I can apply it to my larger dataset.  Thanks

File Structure:
0 Kudos
1 Reply
Occasional Contributor II
It can be done with python using os.listdir() to list the folders and arcpy.ListFeatureClasses() to grab all the feature classes in the folder:

Something like:

import arcpy
import os

mainLoc = r"c:\testing"
outputLoc = r"c:\finalLocation\Output.gdb" #Note: ouput location should be outside the "testing" folder or additional runs will include any previously generated output
FCList = []

for dir in os.listdir(mainLoc):
    arcpy.env.workspace = dir
    FCsubList = arcpy.ListFeatureClasses("*_CadastralPolygons.shp", "Polygon")

arcpy.Merge_management(FCList, outputLoc)
0 Kudos