[GIS] Loading multiple shapefiles from +1000 subfolders

arcgis-10.3arcgis-desktoparcmapload-them-all-pluginmerge

I have a WATERBODY shapefile for each region within each province in Canada. The region subfolders also contain many other features that I do not need. I am trying to upload and merge all the WATERBODY shapefiles of Canada, without searching through each of the ~1000 subfolders. Rumour has it this is possible in the 'Load Them All' plugin for QGIS but I am using ArcMap 10.3.1.

I have no knowledge of Python coding and that seems to be the answer to similar questions from 2012 on forums. Is there an ArcMap equivalent? Is there an easier way than learning Python coding for this one step?

Best Answer

The good news is you can achieve this without 'learning' Python coding. The bad news is, you still need to use Python code!

Open a fresh instance of ArcMap, go to the Geoprocessing menu and select Python. This will open the Python window. Since there are so many datasets to be added (and therefore drawn), I recommend pausing the data view before proceeding.

Type the following code into the Python window. Make sure you indent your code in the same way (use four spaces for a single indent, 8 for a double etc). You will need to replace the path C:/DATA with the path of your base folder, making sure you use forward slashes "/" in the path rather than backslashes (don't ask or you will be learning coding!). You will need to replace the text WATERBODY with some other characters which occur in the filename of every waterbody dataset (do this in CAPTIALS). Once you've altered the path, press Enter

import os

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]

workspace = "C:/DATA"    #place the path to your data inside the quote marks, remembering to use forward slashes
feature_classes = []

walk = arcpy.da.Walk(workspace, datatype="FeatureClass", type="Polygon")

for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))

for dataset in feature_classes:
    dataset = dataset.upper()
    if "WATERBODY" in dataset:
        add_layer = arcpy.mapping.Layer(dataset)
        arcpy.mapping.AddLayer(df, add_layer)

Your computer might have to think about thinks for a few seconds/minutes. Once it has finished, you should have all the datasets which are located within that folder and sub-folders, shown in the Table of Contents. Now it's up to you what you want to do with them!

Related Question