Select to view content in your preferred language

Batch merge multiple shapefiles - with confusing file structure

1658
1
12-24-2013 08:36 AM
NathanielClement
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
MattSayler
Frequent Contributor
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")
    FCList.append(FCsubList)

arcpy.Merge_management(FCList, outputLoc)
0 Kudos